fix: use publicDir & base for 'include your base in url' error message (#9841)

Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>
Co-authored-by: bluwy <bjornlu.dev@gmail.com>
This commit is contained in:
Kristian Binau 2024-02-01 16:13:58 +01:00 committed by GitHub
parent 2f448a27f2
commit 27ea080e24
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
"astro": patch
---
Makes the warning clearer when having a custom `base` and requesting a public URL without it

View file

@ -3,6 +3,8 @@ import type { AstroSettings } from '../@types/astro.js';
import { bold } from 'kleur/colors';
import * as fs from 'node:fs';
import path from 'node:path';
import { appendForwardSlash } from '@astrojs/internal-helpers/path';
import type { Logger } from '../core/logger/core.js';
import notFoundTemplate, { subpathNotUsedTemplate } from '../template/4xx.js';
import { writeHtmlResponse } from './response.js';
@ -52,11 +54,15 @@ export function baseMiddleware(
const publicPath = new URL('.' + req.url, config.publicDir);
fs.stat(publicPath, (_err, stats) => {
if (stats) {
const expectedLocation = new URL('.' + url, devRootURL).pathname;
const publicDir = appendForwardSlash(
path.posix.relative(config.root.pathname, config.publicDir.pathname)
);
const expectedLocation = new URL(devRootURL.pathname + url, devRootURL).pathname;
logger.error(
'router',
`Request URLs for ${bold(
'public/'
publicDir
)} assets must also include your base. "${expectedLocation}" expected, but received "${url}".`
);
const html = subpathNotUsedTemplate(devRoot, pathname);