question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

getServerSideProps + Link as prop generating incorrect data fetching URL

See original GitHub issue

Bug report

Describe the bug

getServerSideProps with:

  • a custom server
  • not using NextJS’ builtin dynamic routes
  • pretty URLs (using the as prop on Link)

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:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Timercommented, Jun 29, 2020

This should be fixed on canary!

0reactions
balazsorban44commented, Jan 29, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Data Fetching: getServerSideProps - Next.js
When exporting a function called getServerSideProps (Server-Side Rendering) from a page, Next.js will pre-render this page on each request using the data ......
Read more >
Refreshing Server-Side Props - Next.js - Josh W Comeau
In the page component, use a library like SWR to track the data. It'll be initialized from the server-side props, but connect to...
Read more >
Nextjs failing on getServerSideProps for no reason
When sending https requests, try adding async / await to handle Promises that axios or fetch or httpRequest returns. Here is an example....
Read more >
Testing and error handling patterns in Next.js - LogRocket Blog
The getStaticProps function should return an object containing either props , redirect , or notFound . Setting the notFound prop to true in...
Read more >
What is Next.js getInitialProps and getServerSideProps? | refine
Using getInitialProps for data fetching and SSR in Next.js is outdated and deprecated because its behavior can be confusing for beginners and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found