diff --git a/packages/gallery/package.json b/packages/gallery/package.json index 06fbc37..cbb0511 100644 --- a/packages/gallery/package.json +++ b/packages/gallery/package.json @@ -30,11 +30,10 @@ "semantic-ui-site": "^2.2.12", "style-loader": "^0.19.0", "styletron": "^2.5.1", - "styletron-utils": "^2.5.4", - "webpack": "~3.8.1", - "webpack-dev-server": "~2.9.2" + "styletron-utils": "^2.5.4" }, "devDependencies": { - "webpack-dev-server": "~2.4.2" + "webpack": "~3.8.1", + "webpack-dev-server": "~2.9.2" } } diff --git a/packages/gallery/src/app.js b/packages/gallery/src/app.js index 8e1c245..2bdad01 100644 --- a/packages/gallery/src/app.js +++ b/packages/gallery/src/app.js @@ -4,6 +4,7 @@ import * as styles from './app.css'; import { GalleryView } from './interface/gallery.js'; import { router } from './services/router.js'; import { streamConfig } from './utils/event.js'; +import { log } from './services/console.js'; import { EventEmitter } from 'events'; @@ -11,8 +12,19 @@ EventEmitter.defaultMaxListeners = 1000; // https://github.com/pouchdb/pouchdb/i config({ stream: streamConfig }); -// Attach our root view to the DOM -createView(GalleryView, {}).mount(document.body); +function go() { + // Attach our root view to the DOM + createView(GalleryView, {}).mount(document.body); -// Start the router -router.start('home'); + // Start the router + router.start('home'); +} + +if ('serviceWorker' in navigator) { + navigator.serviceWorker + .register('/assets/sw.bundle.js', { scope: '/' }) + .then(go) + .catch(go); +} else { + go(); +} diff --git a/packages/gallery/src/sw.js b/packages/gallery/src/sw.js new file mode 100644 index 0000000..d2fb136 --- /dev/null +++ b/packages/gallery/src/sw.js @@ -0,0 +1,35 @@ +import { FileType } from './data/file.js'; + +self.addEventListener('fetch', event => { + const requestUrl = new URL(event.request.url); + + if (requestUrl.pathname && requestUrl.pathname.startsWith('/file/')) { + event.respondWith( + FileType.getFromURL(requestUrl.pathname) + .then( + data => + data + ? new Response(data, { + status: 200, + statusText: 'OK', + headers: { + 'Content-Type': data.mimetype + } + }) + : new Response(null, { + status: 404, + statusText: 'NOT FOUND', + headers: {} + }) + ) + .catch( + () => + new Response(null, { + status: 404, + statusText: 'NOT FOUND', + headers: {} + }) + ) + ); + } +}); diff --git a/packages/gallery/webpack.config.js b/packages/gallery/webpack.config.js index ccada62..378efa2 100644 --- a/packages/gallery/webpack.config.js +++ b/packages/gallery/webpack.config.js @@ -5,7 +5,8 @@ const ExtractTextPlugin = require('extract-text-webpack-plugin'); module.exports = { context: path.resolve(__dirname, './src'), entry: { - app: './app.js' + app: './app.js', + sw: './sw.js' }, output: { path: path.resolve(__dirname, './dist'), @@ -13,7 +14,10 @@ module.exports = { publicPath: '/assets' }, devServer: { - contentBase: path.resolve(__dirname, './src') + contentBase: path.resolve(__dirname, './src'), + headers: { + 'Service-Worker-Allowed': '/' + } }, module: { rules: [ @@ -27,6 +31,7 @@ module.exports = { ] }, plugins: [ + new webpack.optimize.CommonsChunkPlugin({}), new webpack.DefinePlugin({ __DEV__: true }),