diff --git a/.changeset/bright-glasses-jump.md b/.changeset/bright-glasses-jump.md new file mode 100644 index 000000000..fe0fb8c4e --- /dev/null +++ b/.changeset/bright-glasses-jump.md @@ -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. \ No newline at end of file diff --git a/packages/astro/src/core/dev/index.ts b/packages/astro/src/core/dev/index.ts index bd8c148b2..04b0abf7a 100644 --- a/packages/astro/src/core/dev/index.ts +++ b/packages/astro/src/core/dev/index.ts @@ -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 }); }