Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sw-precache, register serviceWorker #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"mockdate": "^2.0.2",
"nodemon": "^1.12.0",
"react-test-renderer": "^15.6.1",
"sw-precache-webpack-plugin": "^0.11.4",
"webpack": "^3.5.5",
"webpack-dev-server": "^2.7.1"
}
Expand Down
29 changes: 29 additions & 0 deletions src/helpers/registerServiceWorker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react';

export default (ComposedComponent) => {
return class RegisterServiceWorker extends React.Component {
static displayName = `RegisterServiceWorker(${ComposedComponent.displayName})`


async componentDidMount() {
if (process.env.NODE_ENV === 'production') {
if ('serviceWorker' in navigator) {
navigator.serviceWorker
.register('/service-worker.js')
.then(registration => {
console.log('service worker registration successful');
})
.catch(err => {
console.warn('service worker registration failed');
});
}
}
}

render() {
return (
<ComposedComponent {...this.props} />
);
}
}
}
39 changes: 21 additions & 18 deletions src/next.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const SWPrecacheWebpackPlugin = require("sw-precache-webpack-plugin");

module.exports = {
exportPathMap: function exportPathMap() {
Expand Down Expand Up @@ -33,23 +34,25 @@ module.exports = {
// '/p/481': { page: '/post', query: { id: '481' } },
};
},
// webpack: (config, { dev }) => {
// // Perform customizations to webpack config
// if (!dev) {
// config.module.rules.push({
// test: /\.(css|ico|gif)$/,
// use: [
// {
// loader: 'file-loader',
// options: {
// outputPath: 'static/',
// },
// },
// ],
// });
// }
webpack: (config, { dev }) => {
if (dev) {
return config;
}

// // Important: return the modified config
// return config;
// },
config.plugins.push(
new SWPrecacheWebpackPlugin({
minify: true,
verbose: true,
staticFileGlobsIgnorePatterns: [/\.next\//],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is to ignore all static files served by next

runtimeCaching: [
{
handler: 'networkFirst',
urlPattern: /^https?.*/
}
]
})
);

return config;
}
};
5 changes: 3 additions & 2 deletions src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Main from '../layouts/Main';
import NewsFeed from '../components/presentational/NewsFeed';
import NewsFeedApolloHOC from '../components/container/NewsFeedWithApolloRenderer';
import withData from '../helpers/withData';
import registerServiceWorker from '../helpers/registerServiceWorker';

const POSTS_PER_PAGE = 30;

Expand Down Expand Up @@ -45,7 +46,7 @@ const TopNewsFeed = graphql(query, {
}),
})(NewsFeedApolloHOC);

export default withData((props) => {
export default withData(registerServiceWorker((props) => {
const pageNumber = (props.url.query && +props.url.query.p) || 0;
return (
<Main currentURL={props.url.pathname}>
Expand All @@ -57,4 +58,4 @@ export default withData((props) => {
/>
</Main>
);
});
}));
6 changes: 6 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from 'path';
import dotenv from 'dotenv/config';
import express from 'express';
import cookieParser from 'cookie-parser';
Expand Down Expand Up @@ -148,6 +149,11 @@ app.prepare()
app.render(req, res, actualPage);
});

server.get('/service-worker.js', (req, res) => {
const swPath = path.join(__dirname, '.next', '/service-worker.js');
app.serveStatic(req, res, swPath);
})

server.get('*', (req, res) => handle(req, res));

/* END EXPRESS ROUTES */
Expand Down
Loading