mirror of
https://github.com/withastro/astro.git
synced 2025-01-06 22:10:10 -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 type { AstroSettings } from '../@types/astro';
|
||||||
|
|
||||||
import * as fs from 'fs';
|
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 notFoundTemplate, { subpathNotUsedTemplate } from '../template/4xx.js';
|
||||||
import { log404 } from './common.js';
|
import { log404 } from './common.js';
|
||||||
import { writeHtmlResponse } from './response.js';
|
import { writeHtmlResponse } from './response.js';
|
||||||
|
@ -13,7 +13,8 @@ export function baseMiddleware(
|
||||||
): vite.Connect.NextHandleFunction {
|
): vite.Connect.NextHandleFunction {
|
||||||
const { config } = settings;
|
const { config } = settings;
|
||||||
const site = config.site ? new URL(config.base, config.site) : undefined;
|
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) {
|
return function devBaseMiddleware(req, res, next) {
|
||||||
const url = req.url!;
|
const url = req.url!;
|
||||||
|
@ -46,9 +47,12 @@ export function baseMiddleware(
|
||||||
const publicPath = new URL('.' + req.url, config.publicDir);
|
const publicPath = new URL('.' + req.url, config.publicDir);
|
||||||
fs.stat(publicPath, (_err, stats) => {
|
fs.stat(publicPath, (_err, stats) => {
|
||||||
if (stats) {
|
if (stats) {
|
||||||
log404(logging, pathname);
|
const expectedLocation = new URL('.' + url, devRootURL).pathname;
|
||||||
const html = subpathNotUsedTemplate(devRoot, 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.`);
|
||||||
return writeHtmlResponse(res, 404, html);
|
res.writeHead(301, {
|
||||||
|
'Location': expectedLocation
|
||||||
|
});
|
||||||
|
res.end();
|
||||||
} else {
|
} else {
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
|
|
|
@ -187,7 +187,8 @@ describe('dev container', () => {
|
||||||
container.handle(r.req, r.res);
|
container.handle(r.req, r.res);
|
||||||
await r.done;
|
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