diff --git a/.gitignore b/.gitignore index f20aae2..7800c23 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ node_modules yarn.lock cypress/screenshots templates/.* -dist \ No newline at end of file +dist +app/manifest \ No newline at end of file diff --git a/app/client.js b/app/client.js new file mode 100644 index 0000000..05d30aa --- /dev/null +++ b/app/client.js @@ -0,0 +1,7 @@ +import { init } from 'sapper/runtime.js'; +import { routes } from './manifest/client.js'; + +// `routes` is an array of route objects injected by Sapper +init(document.querySelector('#sapper'), routes); + +if (module.hot) module.hot.accept(); \ No newline at end of file diff --git a/server.js b/app/server.js similarity index 52% rename from server.js rename to app/server.js index cd79704..0af7864 100644 --- a/server.js +++ b/app/server.js @@ -1,13 +1,16 @@ -const fs = require('fs'); -const app = require('express')(); -const compression = require('compression'); -const sapper = require('sapper'); -const static = require('serve-static'); +import fs from 'fs'; +import express from 'express'; +import compression from 'compression'; +import sapper from 'sapper'; +import serve from 'serve-static'; +import fetch from 'node-fetch'; +import { routes } from './manifest/server.js'; + +const app = express(); const { PORT = 3000 } = process.env; // this allows us to do e.g. `fetch('/api/blog-posts')` on the server -const fetch = require('node-fetch'); global.fetch = (url, opts) => { if (url[0] === '/') url = `http://localhost:${PORT}${url}`; return fetch(url, opts); @@ -15,9 +18,11 @@ global.fetch = (url, opts) => { app.use(compression({ threshold: 0 })); -app.use(static('assets')); +app.use(serve('assets')); -app.use(sapper()); +app.use(sapper({ + routes +})); app.listen(PORT, () => { console.log(`listening on port ${PORT}`); diff --git a/templates/service-worker.js b/app/service-worker.js similarity index 90% rename from templates/service-worker.js rename to app/service-worker.js index cccb12a..542371d 100644 --- a/templates/service-worker.js +++ b/app/service-worker.js @@ -1,15 +1,12 @@ -const timestamp = '__timestamp__'; +import { timestamp, assets, shell, routes } from './manifest/service-worker.js'; + const ASSETS = `cache${timestamp}`; // `shell` is an array of all the files generated by webpack, // `assets` is an array of everything in the `assets` directory -const to_cache = __shell__.concat(__assets__); +const to_cache = shell.concat(assets); const cached = new Set(to_cache); -// `routes` is an array of `{ pattern: RegExp }` objects that -// match the pages in your app -const routes = __routes__; - self.addEventListener('install', event => { event.waitUntil( caches diff --git a/templates/2xx.html b/app/template.html similarity index 89% rename from templates/2xx.html rename to app/template.html index 5dc665a..a7e7d49 100644 --- a/templates/2xx.html +++ b/app/template.html @@ -10,9 +10,9 @@ -
Could not %sapper.method% %sapper.url%
- - %sapper.scripts% - -