Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix middleware fallback: false case #69799

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/next/src/server/lib/router-utils/resolve-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
53 changes: 53 additions & 0 deletions test/e2e/middleware-general/app/pages/ssg-fallback-false/[slug].js
Original file line number Diff line number Diff line change
@@ -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 (
<>
<p id="ssg">/blog/[slug]</p>
<p id="query">{JSON.stringify(router.query)}</p>
<p id="pathname">{router.pathname}</p>
<p id="as-path">{asPath}</p>
<p id="props">{JSON.stringify(props)}</p>
</>
)
}

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,
}
}
9 changes: 9 additions & 0 deletions test/e2e/middleware-general/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")')
Expand Down
Loading