Skip to content

Commit

Permalink
Handling of static assets with paths.base (#3346)
Browse files Browse the repository at this point in the history
* Fix svelte-kit dev/preview when config.kit.paths.base is set

* add test of truncated base

* changeset

* failing test of requesting a static asset with basepath prefix

* Serve static assets from /basepath in svelte-kit dev

* Add changeset

* simplify - only mutate req if necessary

* assets_path -> assets

* doh

* check file is a file and not a directory

Co-authored-by: Andrew Soutar <andrew@andrewsoutar.com>
  • Loading branch information
Rich-Harris and andrewsoutar authored Jan 15, 2022
1 parent eddea57 commit d6ddf3c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/tender-actors-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Handle static assets with /basepath in svelte-kit dev
1 change: 0 additions & 1 deletion packages/kit/src/core/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ export async function dev({ cwd, port, host, https, config }) {
}),
await create_plugin(config, cwd)
],
publicDir: config.kit.files.assets,
base: '/'
});

Expand Down
37 changes: 30 additions & 7 deletions packages/kit/src/core/dev/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs';
import path from 'path';
import { URL } from 'url';
import colors from 'kleur';
import sirv from 'sirv';
import { respond } from '../../runtime/server/index.js';
import { __fetch_polyfill } from '../../install-fetch.js';
import { create_app } from '../create_app/index.js';
Expand Down Expand Up @@ -126,6 +127,14 @@ export async function create_plugin(config, cwd) {
vite.watcher.on('add', update_manifest);
vite.watcher.on('remove', update_manifest);

const assets = config.kit.paths.assets ? SVELTE_KIT_ASSETS : config.kit.paths.base;
const asset_server = sirv(config.kit.files.assets, {
dev: true,
etag: true,
maxAge: 0,
extensions: []
});

return () => {
remove_html_middlewares(vite.middlewares);

Expand All @@ -134,8 +143,24 @@ export async function create_plugin(config, cwd) {
if (!req.url || !req.method) throw new Error('Incomplete request');
if (req.url === '/favicon.ico') return not_found(res);

const parsed = new URL(req.url, 'http://localhost/');
if (!parsed.pathname.startsWith(config.kit.paths.base)) return not_found(res);
const url = new URL(
`${vite.config.server.https ? 'https' : 'http'}://${req.headers.host}${req.url}`
);

const decoded = decodeURI(url.pathname);

if (decoded.startsWith(assets)) {
const pathname = decoded.slice(assets.length);
const file = config.kit.files.assets + pathname;

if (fs.existsSync(file) && !fs.statSync(file).isDirectory()) {
req.url = encodeURI(pathname); // don't need query/hash
asset_server(req, res);
return;
}
}

if (!decoded.startsWith(config.kit.paths.base)) return not_found(res);

/** @type {Partial<import('types/internal').Hooks>} */
const user_hooks = resolve_entry(config.kit.files.hooks)
Expand Down Expand Up @@ -179,7 +204,7 @@ export async function create_plugin(config, cwd) {

paths.set_paths({
base: config.kit.paths.base,
assets: config.kit.paths.assets ? SVELTE_KIT_ASSETS : config.kit.paths.base
assets
});

let body;
Expand All @@ -193,9 +218,7 @@ export async function create_plugin(config, cwd) {

const rendered = await respond(
{
url: new URL(
`${vite.config.server.https ? 'https' : 'http'}://${req.headers.host}${req.url}`
),
url,
headers: /** @type {import('types/helper').RequestHeaders} */ (req.headers),
method: req.method,
rawBody: body
Expand All @@ -218,7 +241,7 @@ export async function create_plugin(config, cwd) {
method_override: config.kit.methodOverride,
paths: {
base: config.kit.paths.base,
assets: config.kit.paths.assets ? SVELTE_KIT_ASSETS : config.kit.paths.base
assets
},
prefix: '',
prerender: config.kit.prerender.enabled,
Expand Down
1 change: 1 addition & 0 deletions packages/kit/test/apps/options-2/static/answer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
42
5 changes: 5 additions & 0 deletions packages/kit/test/apps/options-2/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ test.describe.parallel('paths.base', () => {
await page.goto('/basepath');
expect(await page.textContent('h1')).toBe('Hello');
});

test('serves assets from /basepath', async ({ request }) => {
const response = await request.get('/basepath/answer.txt');
expect(await response.text()).toBe('42');
});
});

test.describe.parallel('Service worker', () => {
Expand Down

0 comments on commit d6ddf3c

Please sign in to comment.