diff --git a/packages/next/src/server/lib/router-utils/resolve-routes.ts b/packages/next/src/server/lib/router-utils/resolve-routes.ts index f846853a58010..f12a3d2bc3c1e 100644 --- a/packages/next/src/server/lib/router-utils/resolve-routes.ts +++ b/packages/next/src/server/lib/router-utils/resolve-routes.ts @@ -474,6 +474,9 @@ export function getResolveRoutes( throw new Error(`Failed to initialize render server "middleware"`) } + addRequestMeta(req, 'invokePath', '') + addRequestMeta(req, 'invokeOutput', '') + addRequestMeta(req, 'invokeQuery', {}) addRequestMeta(req, 'middlewareInvoke', true) debug('invoking middleware', req.url, req.headers) diff --git a/test/e2e/middleware-general/app/pages/ssg-fallback-false/[slug].js b/test/e2e/middleware-general/app/pages/ssg-fallback-false/[slug].js new file mode 100644 index 0000000000000..ac8f4401f31db --- /dev/null +++ b/test/e2e/middleware-general/app/pages/ssg-fallback-false/[slug].js @@ -0,0 +1,53 @@ +import { useRouter } from 'next/router' +import { useEffect } from 'react' +import { useState } from 'react' + +export default function Page(props) { + const router = useRouter() + const [asPath, setAsPath] = useState( + router.isReady ? router.asPath : router.href + ) + + if (!props.params) { + console.error('props', props) + throw new Error('missing props!!!') + } + + useEffect(() => { + if (router.isReady) { + setAsPath(router.asPath) + } + }, [router.asPath, router.isReady]) + + return ( + <> +
/blog/[slug]
+{JSON.stringify(router.query)}
+{router.pathname}
+{asPath}
+{JSON.stringify(props)}
+ > + ) +} + +export function getStaticProps({ params }) { + if (params.slug.includes('not-found')) { + return { + notFound: true, + } + } + + return { + props: { + now: Date.now(), + params, + }, + } +} + +export function getStaticPaths() { + return { + paths: ['/ssg-fallback-false/first', '/ssg-fallback-false/hello'], + fallback: false, + } +} diff --git a/test/e2e/middleware-general/test/index.test.ts b/test/e2e/middleware-general/test/index.test.ts index 7101e65349adc..80488e2b596f0 100644 --- a/test/e2e/middleware-general/test/index.test.ts +++ b/test/e2e/middleware-general/test/index.test.ts @@ -102,6 +102,15 @@ describe('Middleware Runtime', () => { } function runTests({ i18n }: { i18n?: boolean }) { + it('should handle 404 on fallback: false route correctly', async () => { + const res = await next.fetch('/ssg-fallback-false/first') + expect(res.status).toBe(200) + expect(await res.text()).toContain('blog') + + const res2 = await next.fetch('/ssg-fallback-false/non-existent') + expect(res2.status).toBe(404) + }) + it('should work with notFound: true correctly', async () => { const browser = await next.browser('/ssr-page') await browser.eval('window.next.router.push("/ssg/not-found-1")')