Skip to content

Commit

Permalink
fix: allow importing assets while using base path
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Jan 23, 2023
1 parent 34be9ad commit c59283a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-students-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: allow importing assets while using base path
6 changes: 5 additions & 1 deletion packages/kit/src/exports/vite/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,24 +364,28 @@ export async function dev(vite, vite_config, svelte_config) {
/** @type {function} */ (middleware.handle).name === 'viteServeStaticMiddleware'
);

// Vite will give a 403 on URLs like /test, /static, and /package.json preventing us from
// serving routes with those names. See https://github.com/vitejs/vite/issues/7363
remove_static_middlewares(vite.middlewares);

vite.middlewares.use(async (req, res) => {
// Vite's base middleware strips out the base path. Restore it
const original_url = req.url;
req.url = req.originalUrl;
try {
const base = `${vite.config.server.https ? 'https' : 'http'}://${
req.headers[':authority'] || req.headers.host
}`;

const decoded = decodeURI(new URL(base + req.url).pathname);
const file = posixify(path.resolve(decoded.slice(1)));
const file = posixify(path.resolve(decoded.slice(svelte_config.kit.paths.base.length + 1)));
const is_file = fs.existsSync(file) && !fs.statSync(file).isDirectory();
const allowed =
!vite_config.server.fs.strict ||
vite_config.server.fs.allow.some((dir) => file.startsWith(dir));

if (is_file && allowed) {
req.url = original_url;
// @ts-expect-error
serve_static_middleware.handle(req, res);
return;
Expand Down
1 change: 1 addition & 0 deletions packages/kit/test/apps/options/source/pages/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello there world
24 changes: 17 additions & 7 deletions packages/kit/test/apps/options/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ test.describe('base path', () => {
);
});

if (process.env.DEV) {
test('serves files in source directory', async ({ request, javaScriptEnabled }) => {
if (!javaScriptEnabled) return;

const response = await request.get('/path-base/source/pages/test.txt');
expect(response.ok()).toBe(true);
expect(await response.text()).toBe('hello there world\n');
});
}

test('sets_paths', async ({ page }) => {
await page.goto('/path-base/base/');
expect(await page.textContent('[data-source="base"]')).toBe('/path-base');
Expand Down Expand Up @@ -234,14 +244,14 @@ test.describe('trailingSlash', () => {
});
});

test.describe('serviceWorker', () => {
if (process.env.DEV) return;

test('does not register service worker if none created', async ({ page }) => {
await page.goto('/path-base/');
expect(await page.content()).not.toMatch('navigator.serviceWorker');
if (!process.env.DEV) {
test.describe('serviceWorker', () => {
test('does not register service worker if none created', async ({ page }) => {
await page.goto('/path-base/');
expect(await page.content()).not.toMatch('navigator.serviceWorker');
});
});
});
}

test.describe('Vite options', () => {
test('Respects --mode', async ({ page }) => {
Expand Down

0 comments on commit c59283a

Please sign in to comment.