getServerSideProps + Link as prop generating incorrect data fetching URL
See original GitHub issueBug report
Describe the bug
getServerSideProps
with:
- a custom server
- not using NextJS’ builtin dynamic routes
- pretty URLs (using the
as
prop onLink
)
fails to generate a working data fetching URL
To Reproduce
Let’s say we have a couple top-level pages (categories) on our site: /fluff/
and /stuff/
. The NextJS page used to render these is category.js
export default Category = ({ category }) => {
return <div>You are on {category.name}</div>
}
export async function getServerSideProps() {
return { props: { category: API.getCategory(ctx.query.slug) } }
}
I use Link
components like <Link as='/fluff' href='/category?category=fluff'>...</Link>
. I’m not using NextJS’ builtin dynamic routing.
The generated data fetching URL is /_next/data/BUILD_ID/fluff.json
whereas it should be /_next/data/BUILD_ID/category.json?category=fluff
. The former fails, as there’s no page explicitly named “fluff”, the latter works as expected.
It should be noted that if you do not specify the as
prop, the URL is generated properly.
Also note that that it does not matter if you use { pathname: '/category', query: { category: 'fluff' } }
or the syntax specified above.
Our custom server, using express, references these pages like so:
const app = next()
const handler = app.getRequestHandler()
app.prepare().then(() => {
const server = express()
server.get('/:category(fluff|stuff)', (req, res) => {
// note that req.params.category = fluff | stuff and is passed through
// to getServerSideProps via ctx.query.category
app.render(req, res, '/category', { ...req.query, ...req.params })
})
server.use((req, res) => handler(req, res))
})
But as mentioned above, data fetching works with a differently generated URL.
Expected behavior
Data fetching should still work using the as
prop.
System information
- Version of Next.js: 9.3.2
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
This should be fixed on canary!
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.