mirror of
https://github.com/withastro/astro.git
synced 2024-12-23 21:53:55 -05:00
Change 404 to 301 for omitting base for items in public (#5342)
This commit is contained in:
parent
301187e2f3
commit
1e6bbb2924
2 changed files with 11 additions and 6 deletions
|
@ -2,7 +2,7 @@ import type * as vite from 'vite';
|
|||
import type { AstroSettings } from '../@types/astro';
|
||||
|
||||
import * as fs from 'fs';
|
||||
import { LogOptions } from '../core/logger/core.js';
|
||||
import { LogOptions, warn } from '../core/logger/core.js';
|
||||
import notFoundTemplate, { subpathNotUsedTemplate } from '../template/4xx.js';
|
||||
import { log404 } from './common.js';
|
||||
import { writeHtmlResponse } from './response.js';
|
||||
|
@ -13,7 +13,8 @@ export function baseMiddleware(
|
|||
): vite.Connect.NextHandleFunction {
|
||||
const { config } = settings;
|
||||
const site = config.site ? new URL(config.base, config.site) : undefined;
|
||||
const devRoot = site ? site.pathname : new URL(config.base, 'http://localhost').pathname;
|
||||
const devRootURL = new URL(config.base, 'http://localhost');
|
||||
const devRoot = site ? site.pathname : devRootURL.pathname;
|
||||
|
||||
return function devBaseMiddleware(req, res, next) {
|
||||
const url = req.url!;
|
||||
|
@ -46,9 +47,12 @@ export function baseMiddleware(
|
|||
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);
|
||||
const expectedLocation = new URL('.' + url, devRootURL).pathname;
|
||||
warn(logging, 'dev', `Requests for items in your public folder must also include your base. ${url} should be ${expectedLocation}. Omitting the base will break in production.`);
|
||||
res.writeHead(301, {
|
||||
'Location': expectedLocation
|
||||
});
|
||||
res.end();
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
|
|
|
@ -187,7 +187,8 @@ describe('dev container', () => {
|
|||
container.handle(r.req, r.res);
|
||||
await r.done;
|
||||
|
||||
expect(r.res.statusCode).to.equal(404);
|
||||
expect(r.res.statusCode).to.equal(301);
|
||||
expect(r.res.getHeader('location')).to.equal('/sub/test.txt');
|
||||
}
|
||||
);
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue