Skip to content

Commit

Permalink
404 when not using subpath for items in public in dev (#5328)
Browse files Browse the repository at this point in the history
* 404 when not using subpath for items in public

* Adding a changeset
  • Loading branch information
matthewp authored Nov 9, 2022
1 parent 48bde70 commit bcd0f8f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .changeset/plenty-eyes-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'astro': patch
---

404 when not using subpath for items in public in dev

Previously if using a base like `base: '/subpath/` you could load things from the root, which would break in prod. Now you must include the subpath.
18 changes: 15 additions & 3 deletions packages/astro/src/vite-plugin-astro-server/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ import { LogOptions } from '../core/logger/core.js';
import notFoundTemplate, { subpathNotUsedTemplate } from '../template/4xx.js';
import { log404 } from './common.js';
import { writeHtmlResponse } from './response.js';
import * as fs from 'fs';

export function baseMiddleware(
settings: AstroSettings,
logging: LogOptions
): vite.Connect.NextHandleFunction {
const { config } = settings;
const site = config.site ? new URL(config.base, config.site) : undefined;
const devRoot = site ? site.pathname : '/';
const devRoot = site ? site.pathname : new URL(config.base, 'http://localhost').pathname;

return function devBaseMiddleware(req, res, next) {
const url = req.url!;

const pathname = decodeURI(new URL(url, 'http://vitejs.dev').pathname);
const pathname = decodeURI(new URL(url, 'http://localhost').pathname);


if (pathname.startsWith(devRoot)) {
req.url = url.replace(devRoot, '/');
Expand All @@ -41,6 +43,16 @@ export function baseMiddleware(
return writeHtmlResponse(res, 404, html);
}

next();
// Check to see if it's in public and if so 404
const publicPath = new URL('.' + req.url, config.publicDir);
fs.stat(publicPath, (_err, stats) => {
if(stats) {
log404(logging, pathname);
const html = subpathNotUsedTemplate(devRoot, pathname);
return writeHtmlResponse(res, 404, html);
} else {
next();
}
});
};
}
1 change: 1 addition & 0 deletions packages/astro/test/fixtures/alias/public/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is a test
46 changes: 46 additions & 0 deletions packages/astro/test/units/dev/dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,50 @@ describe('dev container', () => {
}
);
});

it('items in public/ are not available from root when using a base', async () => {
await runInContainer({
root,
userConfig: {
base: '/sub/'
}
}, async (container) => {
// First try the subpath
let r = createRequestAndResponse({
method: 'GET',
url: '/sub/test.txt',
});

container.handle(r.req, r.res);
await r.done;

expect(r.res.statusCode).to.equal(200);

// Next try the root path
r = createRequestAndResponse({
method: 'GET',
url: '/test.txt',
});

container.handle(r.req, r.res);
await r.done;

expect(r.res.statusCode).to.equal(404);
});
});

it('items in public/ are available from root when not using a base', async () => {
await runInContainer({ root }, async (container) => {
// Try the root path
let r = createRequestAndResponse({
method: 'GET',
url: '/test.txt',
});

container.handle(r.req, r.res);
await r.done;

expect(r.res.statusCode).to.equal(200);
});
});
});

0 comments on commit bcd0f8f

Please sign in to comment.