Safari supports service workers now. It's time to add them.

This commit is contained in:
Timothy Farrell 2017-12-21 22:40:08 -06:00
parent 42b3ce8921
commit 7cb4d5a364
4 changed files with 61 additions and 10 deletions

View File

@ -30,11 +30,10 @@
"semantic-ui-site": "^2.2.12", "semantic-ui-site": "^2.2.12",
"style-loader": "^0.19.0", "style-loader": "^0.19.0",
"styletron": "^2.5.1", "styletron": "^2.5.1",
"styletron-utils": "^2.5.4", "styletron-utils": "^2.5.4"
"webpack": "~3.8.1",
"webpack-dev-server": "~2.9.2"
}, },
"devDependencies": { "devDependencies": {
"webpack-dev-server": "~2.4.2" "webpack": "~3.8.1",
"webpack-dev-server": "~2.9.2"
} }
} }

View File

@ -4,6 +4,7 @@ import * as styles from './app.css';
import { GalleryView } from './interface/gallery.js'; import { GalleryView } from './interface/gallery.js';
import { router } from './services/router.js'; import { router } from './services/router.js';
import { streamConfig } from './utils/event.js'; import { streamConfig } from './utils/event.js';
import { log } from './services/console.js';
import { EventEmitter } from 'events'; import { EventEmitter } from 'events';
@ -11,8 +12,19 @@ EventEmitter.defaultMaxListeners = 1000; // https://github.com/pouchdb/pouchdb/i
config({ stream: streamConfig }); config({ stream: streamConfig });
// Attach our root view to the DOM function go() {
createView(GalleryView, {}).mount(document.body); // Attach our root view to the DOM
createView(GalleryView, {}).mount(document.body);
// Start the router // Start the router
router.start('home'); router.start('home');
}
if ('serviceWorker' in navigator) {
navigator.serviceWorker
.register('/assets/sw.bundle.js', { scope: '/' })
.then(go)
.catch(go);
} else {
go();
}

View File

@ -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: {}
})
)
);
}
});

View File

@ -5,7 +5,8 @@ const ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = { module.exports = {
context: path.resolve(__dirname, './src'), context: path.resolve(__dirname, './src'),
entry: { entry: {
app: './app.js' app: './app.js',
sw: './sw.js'
}, },
output: { output: {
path: path.resolve(__dirname, './dist'), path: path.resolve(__dirname, './dist'),
@ -13,7 +14,10 @@ module.exports = {
publicPath: '/assets' publicPath: '/assets'
}, },
devServer: { devServer: {
contentBase: path.resolve(__dirname, './src') contentBase: path.resolve(__dirname, './src'),
headers: {
'Service-Worker-Allowed': '/'
}
}, },
module: { module: {
rules: [ rules: [
@ -27,6 +31,7 @@ module.exports = {
] ]
}, },
plugins: [ plugins: [
new webpack.optimize.CommonsChunkPlugin({}),
new webpack.DefinePlugin({ new webpack.DefinePlugin({
__DEV__: true __DEV__: true
}), }),