Fix: dev server routing when not using subpath (#1846)
* Fix: dev server routing when not using subpath * Better comment * Adds a changeset * Remove testing file * Rename this.pathname to this.devRoot
This commit is contained in:
parent
750c249c32
commit
0f9c191010
2 changed files with 27 additions and 10 deletions
5
.changeset/bright-glasses-jump.md
Normal file
5
.changeset/bright-glasses-jump.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Fixes routing regression in next.4. Subpath support was inadvertedly prevent any non-index routes from working when not using a subpath.
|
|
@ -60,7 +60,7 @@ export class AstroDevServer {
|
|||
private manifest: ManifestData;
|
||||
private mostRecentRoute?: RouteData;
|
||||
private site: URL | undefined;
|
||||
private pathname: string;
|
||||
private devRoot: string;
|
||||
private url: URL;
|
||||
private origin: string;
|
||||
private routeCache: RouteCache = {};
|
||||
|
@ -73,8 +73,8 @@ export class AstroDevServer {
|
|||
this.port = config.devOptions.port;
|
||||
this.origin = `http://localhost:${this.port}`;
|
||||
this.site = config.buildOptions.site ? new URL(config.buildOptions.site) : undefined;
|
||||
this.pathname = this.site ? this.site.pathname + '/' : '/';
|
||||
this.url = new URL(this.pathname, this.origin);
|
||||
this.devRoot = this.site ? this.site.pathname : '/';
|
||||
this.url = new URL(this.devRoot, this.origin);
|
||||
this.manifest = createRouteManifest({ config });
|
||||
}
|
||||
|
||||
|
@ -201,7 +201,7 @@ export class AstroDevServer {
|
|||
const listen = () => {
|
||||
this.httpServer = this.app.listen(this.port, this.hostname, () => {
|
||||
info(this.logging, 'astro', msg.devStart({ startupTime: performance.now() - devStart }));
|
||||
info(this.logging, 'astro', msg.devHost({ host: `http://${this.hostname}:${this.port}${this.pathname}` }));
|
||||
info(this.logging, 'astro', msg.devHost({ host: `http://${this.hostname}:${this.port}${this.devRoot}` }));
|
||||
resolve();
|
||||
});
|
||||
this.httpServer?.on('error', onError);
|
||||
|
@ -277,10 +277,22 @@ export class AstroDevServer {
|
|||
const reqStart = performance.now();
|
||||
let filePath: URL | undefined;
|
||||
try {
|
||||
let routePathname = pathname.startsWith(this.pathname) ? pathname.substr(this.pathname.length) || '/' : undefined;
|
||||
if (!routePathname) {
|
||||
next();
|
||||
return;
|
||||
let routePathname: string = pathname;
|
||||
// If using a subpath, ensure that the user has included the pathname
|
||||
// such as /blog in the URL.
|
||||
if(this.devRoot !== '/') {
|
||||
if(pathname.startsWith(this.devRoot)) {
|
||||
// This includes the subpath, so strip off the subpath so that
|
||||
// matchRoute finds this route.
|
||||
routePathname = pathname.substr(this.devRoot.length) || '';
|
||||
if(!routePathname.startsWith('/')) {
|
||||
routePathname = '/' + routePathname;
|
||||
}
|
||||
} else {
|
||||
// Not using the subpath, so forward to Vite's middleware
|
||||
next();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const route = matchRoute(routePathname, this.manifest);
|
||||
|
@ -363,8 +375,8 @@ export class AstroDevServer {
|
|||
}
|
||||
// if not found, fall back to default template
|
||||
else {
|
||||
if (pathname === '/' && !pathname.startsWith(this.pathname)) {
|
||||
html = subpathNotUsedTemplate(this.pathname, pathname);
|
||||
if (pathname === '/' && !pathname.startsWith(this.devRoot)) {
|
||||
html = subpathNotUsedTemplate(this.devRoot, pathname);
|
||||
} else {
|
||||
html = notFoundTemplate({ statusCode, title: 'Not found', tabTitle: '404: Not Found', pathname });
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue