Skip to content

Instantly share code, notes, and snippets.

@zackargyle
zackargyle / sample-webpack.js
Created October 12, 2017 20:44
Sample of Pinterest Mweb webpack configuration
const bundles = {
'vendor-mweb': [
'app/mobile/polyfills.js',
'intl',
'normalizr',
'react-dom',
'react-redux',
'react-router-dom',
'react',
'redux'
@zackargyle
zackargyle / sample-sw-caching.js
Created October 12, 2017 20:54
Sample of Pinterest Mweb service worker generation
/* global $VERSION, $Cache, importScripts, WorkboxSW */
importScripts('https://unpkg.com/workbox-sw@1.1.0/build/importScripts/workbox-sw.prod.v1.1.0.js');
// Add app shell to the webpack-generated precache list
$Cache.precache.push({ url: 'sw-shell.html', revision: $VERSION });
// Register precache list with Workbox
const workbox = new WorkboxSW({ handleFetch: true, skipWaiting: true, clientClaim: true });
workbox.precache($Cache.precache);
@zackargyle
zackargyle / sample-sw-generation.js
Created October 12, 2017 20:49
Sample of Pinterest Mweb service worker generation
/* Create a service worker for every locale to precache the locale bundle */
const ServiceWorkerConfigs = locales.reduce((configs, locale) => {
return Object.assign(configs, {
[`mobile-${locale}`]: Object.assign({}, BaseConfig, {
template: path.join(__dirname, 'swTemplates/mobileBase.js'),
cache: {
template: path.join(__dirname, 'swTemplates/mobileCache.js'),
precache: [
'vendor-mweb-.*\\.js$',
'entryChunk-mobile-.*\\.js$',
@zackargyle
zackargyle / sample-codesplitting.js
Last active October 12, 2017 20:44
Sample of Pinterest Mweb code splitting
// Create a loader
const Closeup = () => import(/* webpackChunkName: "CloseupPage" */ 'app/mobile/routes/CloseupPage');
// Register it to the route
route('/pin/:pinId', routes.Closeup, { name: 'Closeup' }),
// Render a react-router-v4 Route with the route bundle loader
<Route exact key="matched-route" path={path} render={matchProps =>
<PageRoute
bundleLoader={loader}
@zackargyle
zackargyle / Homefeed.jsx
Last active September 27, 2017 17:26
Home page layout
const Homefeed = createFeed({
feedKey: 'homefeed',
resourceName: 'UserHomefeedResource',
resourceOptions: () => ({
field_set_key: 'mobile_grid_item',
}),
pullToRefresh: true,
});
const HomePage = (props: Props) => (
type Props = {
match: { params: { username: string } },
resource: { fetching: boolean },
user: User,
};
export default withResource({
name: 'UserResource',
key: 'resource',
options: ({ match }: Props) => ({
@zackargyle
zackargyle / DeferredMount.js
Last active March 28, 2017 18:47
React component for deferring rendering until post-mount.
/*
<DeferredMount ssr />
This component defers rendering of its children until after mounting, good
for below-the-fold content. If the content should be rendered syncronously
on the server you can set the `ssr` prop. If `ssr` is set to true, there
will be a client/server mismatch unless you can somehow determine that we
are hydrating from the server.
Is there a better way than the hack below?
*/
{
caches: {
CACHE_V1: {}
},
clients: [],
notifications: [],
}
const makeServiceWorkerEnv = require('service-worker-mock');
const Response = () => ({ clone: jest.fn() });
const Request = () => ({ url: '/test' });
describe('Service worker', () => {
beforeEach(() => {
Object.assign(global, makeServiceWorkerEnv());
jest.resetModules();
});
const makeServiceWorkerEnv = require('service-worker-mock');
describe('Service worker', () => {
beforeEach(() => {
Object.assign(global, makeServiceWorkerEnv());
jest.resetModules();
});
it('should delete old caches on activate', async () => {
require('../sw.js');