diff --git a/runtime/src/app/app.ts b/runtime/src/app/app.ts index 95b882fd0..89e099dea 100644 --- a/runtime/src/app/app.ts +++ b/runtime/src/app/app.ts @@ -356,7 +356,7 @@ export async function hydrate_target(target: Target): Promise<{ } function load_css(chunk: string) { - const href = `client/${chunk}`; + const href = initial_data.baseUrl + `/client/${chunk}`; if (document.querySelector(`link[href="${href}"]`)) return; return new Promise((fulfil, reject) => { diff --git a/runtime/src/server/middleware/get_page_handler.ts b/runtime/src/server/middleware/get_page_handler.ts index 53f413640..f4367ca32 100644 --- a/runtime/src/server/middleware/get_page_handler.ts +++ b/runtime/src/server/middleware/get_page_handler.ts @@ -311,7 +311,7 @@ export function get_page_handler( }); styles = Array.from(css_chunks) - .map(href => ``) + .map(href => ``) .join('') } else { styles = (css && css.code ? `` : ''); @@ -321,7 +321,7 @@ export function get_page_handler( const nonce_attr = (res.locals && res.locals.nonce) ? ` nonce="${res.locals.nonce}"` : ''; const body = template() - .replace('%sapper.base%', () => ``) + .replace('%sapper.base%', () => req.baseTag ? `` : '') .replace('%sapper.scripts%', () => `${script}`) .replace('%sapper.html%', () => html) .replace('%sapper.head%', () => `${head}`) diff --git a/runtime/src/server/middleware/index.ts b/runtime/src/server/middleware/index.ts index f7bbea4ff..0ed18b6b3 100644 --- a/runtime/src/server/middleware/index.ts +++ b/runtime/src/server/middleware/index.ts @@ -7,15 +7,19 @@ import { get_page_handler } from './get_page_handler'; import { lookup } from './mime'; export default function middleware(opts: { + baseTag: Boolean, session?: (req: Req, res: Res) => any, ignore?: any } = {}) { - const { session, ignore } = opts; + const { session, ignore, baseTag = true } = opts; let emitted_basepath = false; return compose_handlers(ignore, [ (req: Req, res: Res, next: () => void) => { + if (req.baseTag === undefined) { + req.baseTag = baseTag + } if (req.baseUrl === undefined) { let { originalUrl } = req; if (req.url === '/' && originalUrl[originalUrl.length - 1] !== '/') { diff --git a/test/apps/without-basetag/rollup.config.js b/test/apps/without-basetag/rollup.config.js new file mode 100644 index 000000000..66d3e5913 --- /dev/null +++ b/test/apps/without-basetag/rollup.config.js @@ -0,0 +1,58 @@ +import resolve from 'rollup-plugin-node-resolve'; +import replace from 'rollup-plugin-replace'; +import svelte from 'rollup-plugin-svelte'; + +const mode = process.env.NODE_ENV; +const dev = mode === 'development'; + +const config = require('../../../config/rollup.js'); + +export default { + client: { + input: config.client.input(), + output: config.client.output(), + plugins: [ + replace({ + 'process.browser': true, + 'process.env.NODE_ENV': JSON.stringify(mode) + }), + svelte({ + dev, + hydratable: true, + emitCss: true + }), + resolve() + ] + }, + + server: { + input: config.server.input(), + output: config.server.output(), + plugins: [ + replace({ + 'process.browser': false, + 'process.env.NODE_ENV': JSON.stringify(mode) + }), + svelte({ + generate: 'ssr', + dev + }), + resolve({ + preferBuiltins: true + }) + ], + external: ['sirv', 'polka'] + }, + + serviceworker: { + input: config.serviceworker.input(), + output: config.serviceworker.output(), + plugins: [ + resolve(), + replace({ + 'process.browser': true, + 'process.env.NODE_ENV': JSON.stringify(mode) + }) + ] + } +}; \ No newline at end of file diff --git a/test/apps/without-basetag/src/client.js b/test/apps/without-basetag/src/client.js new file mode 100644 index 000000000..6cce7e658 --- /dev/null +++ b/test/apps/without-basetag/src/client.js @@ -0,0 +1,9 @@ +import * as sapper from '@sapper/app'; + +window.start = () => sapper.start({ + target: document.querySelector('#sapper') +}); + +window.prefetchRoutes = () => sapper.prefetchRoutes(); +window.prefetch = href => sapper.prefetch(href); +window.goto = href => sapper.goto(href); \ No newline at end of file diff --git a/test/apps/without-basetag/src/routes/_error.svelte b/test/apps/without-basetag/src/routes/_error.svelte new file mode 100644 index 000000000..e77500131 --- /dev/null +++ b/test/apps/without-basetag/src/routes/_error.svelte @@ -0,0 +1,7 @@ + + +

{status}

+ +

{error.message}

diff --git a/test/apps/without-basetag/src/routes/dirs/bar/[a].svelte b/test/apps/without-basetag/src/routes/dirs/bar/[a].svelte new file mode 100644 index 000000000..f494b27ac --- /dev/null +++ b/test/apps/without-basetag/src/routes/dirs/bar/[a].svelte @@ -0,0 +1,3 @@ +

A page

+ +same segment diff --git a/test/apps/without-basetag/src/routes/dirs/bar/index.svelte b/test/apps/without-basetag/src/routes/dirs/bar/index.svelte new file mode 100644 index 000000000..2cbd4ed1d --- /dev/null +++ b/test/apps/without-basetag/src/routes/dirs/bar/index.svelte @@ -0,0 +1 @@ +

bar

\ No newline at end of file diff --git a/test/apps/without-basetag/src/routes/dirs/foo/[b].svelte b/test/apps/without-basetag/src/routes/dirs/foo/[b].svelte new file mode 100644 index 000000000..f7a165ffb --- /dev/null +++ b/test/apps/without-basetag/src/routes/dirs/foo/[b].svelte @@ -0,0 +1 @@ +

B page

diff --git a/test/apps/without-basetag/src/routes/dirs/foo/index.svelte b/test/apps/without-basetag/src/routes/dirs/foo/index.svelte new file mode 100644 index 000000000..35e8290c3 --- /dev/null +++ b/test/apps/without-basetag/src/routes/dirs/foo/index.svelte @@ -0,0 +1,2 @@ +

foo

+bar \ No newline at end of file diff --git a/test/apps/without-basetag/src/server.js b/test/apps/without-basetag/src/server.js new file mode 100644 index 000000000..e28b8abef --- /dev/null +++ b/test/apps/without-basetag/src/server.js @@ -0,0 +1,16 @@ +import sirv from 'sirv'; +import polka from 'polka'; +import * as sapper from '@sapper/server'; + +import { start, dev } from '../../common.js'; + +const app = polka() + .use( + sirv('static', { dev }), + sapper.middleware({ + baseTag: false + }) + ); + +start(app); + diff --git a/test/apps/without-basetag/src/service-worker.js b/test/apps/without-basetag/src/service-worker.js new file mode 100644 index 000000000..8adb97a43 --- /dev/null +++ b/test/apps/without-basetag/src/service-worker.js @@ -0,0 +1,82 @@ +import * as sapper from '@sapper/service-worker'; + +const ASSETS = `cache${sapper.timestamp}`; + +// `shell` is an array of all the files generated by webpack, +// `files` is an array of everything in the `static` directory +const to_cache = sapper.shell.concat(sapper.files); +const cached = new Set(to_cache); + +self.addEventListener('install', event => { + event.waitUntil( + caches + .open(ASSETS) + .then(cache => cache.addAll(to_cache)) + .then(() => { + self.skipWaiting(); + }) + ); +}); + +self.addEventListener('activate', event => { + event.waitUntil( + caches.keys().then(async keys => { + // delete old caches + for (const key of keys) { + if (key !== ASSETS) await caches.delete(key); + } + + self.clients.claim(); + }) + ); +}); + +self.addEventListener('fetch', event => { + if (event.request.method !== 'GET') return; + + const url = new URL(event.request.url); + + // don't try to handle e.g. data: URIs + if (!url.protocol.startsWith('http')) return; + + // ignore dev server requests + if (url.hostname === self.location.hostname && url.port !== self.location.port) return; + + // always serve assets and webpack-generated files from cache + if (url.host === self.location.host && cached.has(url.pathname)) { + event.respondWith(caches.match(event.request)); + return; + } + + // for pages, you might want to serve a shell `index.html` file, + // which Sapper has generated for you. It's not right for every + // app, but if it's right for yours then uncomment this section + /* + if (url.origin === self.origin && routes.find(route => route.pattern.test(url.pathname))) { + event.respondWith(caches.match('/index.html')); + return; + } + */ + + if (event.request.cache === 'only-if-cached') return; + + // for everything else, try the network first, falling back to + // cache if the user is offline. (If the pages never change, you + // might prefer a cache-first approach to a network-first one.) + event.respondWith( + caches + .open(`offline${sapper.timestamp}`) + .then(async cache => { + try { + const response = await fetch(event.request); + cache.put(event.request, response.clone()); + return response; + } catch(err) { + const response = await cache.match(event.request); + if (response) return response; + + throw err; + } + }) + ); +}); diff --git a/test/apps/without-basetag/src/template.html b/test/apps/without-basetag/src/template.html new file mode 100644 index 000000000..0eb1f3ba4 --- /dev/null +++ b/test/apps/without-basetag/src/template.html @@ -0,0 +1,14 @@ + + + + + + %sapper.base% + %sapper.styles% + %sapper.head% + + +
%sapper.html%
+ %sapper.scripts% + + diff --git a/test/apps/without-basetag/static/.gitkeep b/test/apps/without-basetag/static/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/test/apps/without-basetag/test.ts b/test/apps/without-basetag/test.ts new file mode 100644 index 000000000..dd74106af --- /dev/null +++ b/test/apps/without-basetag/test.ts @@ -0,0 +1,39 @@ +import * as assert from 'assert'; +import {build} from '../../../api'; +import {AppRunner} from '../AppRunner'; + +describe('without-basetag', function() { + this.timeout(10000); + + let r: AppRunner; + + // hooks + before('build app', () => build({ cwd: __dirname })); + before('start runner', async () => { + r = await new AppRunner().start(__dirname); + }); + + after(() => r && r.end()); + + // tests + it('navigates between routes with empty parts', async () => { + await r.load('/dirs/foo'); + await r.sapper.start(); + + assert.equal(await r.text('h1'), 'foo'); + await r.page.click('[href="bar"]'); + await r.wait(); + assert.equal(await r.text('h1'), 'bar'); + }); + + it('navigates between dynamic routes with same segments', async () => { + await r.load('/dirs/bar/xyz'); + await r.sapper.start(); + + assert.equal(await r.text('h1'), 'A page'); + + await r.page.click('[href="../foo/xyz"]'); + await r.wait(); + assert.equal(await r.text('h1'), 'B page'); + }); +});