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 manifest: ManifestData;
|
||||||
private mostRecentRoute?: RouteData;
|
private mostRecentRoute?: RouteData;
|
||||||
private site: URL | undefined;
|
private site: URL | undefined;
|
||||||
private pathname: string;
|
private devRoot: string;
|
||||||
private url: URL;
|
private url: URL;
|
||||||
private origin: string;
|
private origin: string;
|
||||||
private routeCache: RouteCache = {};
|
private routeCache: RouteCache = {};
|
||||||
|
@ -73,8 +73,8 @@ export class AstroDevServer {
|
||||||
this.port = config.devOptions.port;
|
this.port = config.devOptions.port;
|
||||||
this.origin = `http://localhost:${this.port}`;
|
this.origin = `http://localhost:${this.port}`;
|
||||||
this.site = config.buildOptions.site ? new URL(config.buildOptions.site) : undefined;
|
this.site = config.buildOptions.site ? new URL(config.buildOptions.site) : undefined;
|
||||||
this.pathname = this.site ? this.site.pathname + '/' : '/';
|
this.devRoot = this.site ? this.site.pathname : '/';
|
||||||
this.url = new URL(this.pathname, this.origin);
|
this.url = new URL(this.devRoot, this.origin);
|
||||||
this.manifest = createRouteManifest({ config });
|
this.manifest = createRouteManifest({ config });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -201,7 +201,7 @@ export class AstroDevServer {
|
||||||
const listen = () => {
|
const listen = () => {
|
||||||
this.httpServer = this.app.listen(this.port, this.hostname, () => {
|
this.httpServer = this.app.listen(this.port, this.hostname, () => {
|
||||||
info(this.logging, 'astro', msg.devStart({ startupTime: performance.now() - devStart }));
|
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();
|
resolve();
|
||||||
});
|
});
|
||||||
this.httpServer?.on('error', onError);
|
this.httpServer?.on('error', onError);
|
||||||
|
@ -277,10 +277,22 @@ export class AstroDevServer {
|
||||||
const reqStart = performance.now();
|
const reqStart = performance.now();
|
||||||
let filePath: URL | undefined;
|
let filePath: URL | undefined;
|
||||||
try {
|
try {
|
||||||
let routePathname = pathname.startsWith(this.pathname) ? pathname.substr(this.pathname.length) || '/' : undefined;
|
let routePathname: string = pathname;
|
||||||
if (!routePathname) {
|
// If using a subpath, ensure that the user has included the pathname
|
||||||
next();
|
// such as /blog in the URL.
|
||||||
return;
|
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);
|
const route = matchRoute(routePathname, this.manifest);
|
||||||
|
@ -363,8 +375,8 @@ export class AstroDevServer {
|
||||||
}
|
}
|
||||||
// if not found, fall back to default template
|
// if not found, fall back to default template
|
||||||
else {
|
else {
|
||||||
if (pathname === '/' && !pathname.startsWith(this.pathname)) {
|
if (pathname === '/' && !pathname.startsWith(this.devRoot)) {
|
||||||
html = subpathNotUsedTemplate(this.pathname, pathname);
|
html = subpathNotUsedTemplate(this.devRoot, pathname);
|
||||||
} else {
|
} else {
|
||||||
html = notFoundTemplate({ statusCode, title: 'Not found', tabTitle: '404: Not Found', pathname });
|
html = notFoundTemplate({ statusCode, title: 'Not found', tabTitle: '404: Not Found', pathname });
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue