Comments (24)
Do not directly click links or change the address bar to navigate. Use the internal menu on the page to navigate to reproduce the issue.
from next.js.
I can't reproduce... could you take a video snippet of the problem?
from next.js.
wicked.mov
this is wicked, Each time we switch pages, a .json request should be sent, but it isn't. Initially, I thought it was because the DOM wasn't updating, so I dynamically updated a key on the DOM while listening to route changes. However, I later discovered that getServerSideProps
wasn't executing, and the returned data was still the old data.
expected.mov
this is expected
from next.js.
Right, that's strange, I could see the json requests when I tried this the first time... 🤔 let met try again...
from next.js.
Is there any way you might be using shallow navigation, or somehow hijacking GSSP's normal behavior? I remember there's a hook out there to do some mutations to the page, that prevent GSSP from re-running. No Service Workers either?
from next.js.
We haven't made any special modifications. We are currently using middleware, but it doesn't seem related to this issue. This problem still occurs sporadically. You can test it on the links I provided.
import { NextRequest, NextResponse } from 'next/server'
const pagePermissions = ['/account']
export function middleware(request: NextRequest) {
const nextPath = request.nextUrl.pathname
if (
!request.cookies.get('TOKEN')?.value.startsWith('customer') &&
pagePermissions.some(path => nextPath.startsWith(path))
) {
return NextResponse.redirect(new URL(`/login`, request.url))
}
return NextResponse.next()
}
export const config = {
matcher: ['/account/:path*'],
}
Is there any way you might be using shallow navigation, or somehow hijacking GSSP's normal behavior? I remember there's a hook out there to do some mutations to the page, that prevent GSSP from re-running. No Service Workers either?
Can you elaborate on what you said above? @icyJoseph
from next.js.
I swear, I still can't reproduce this... I always get the JSON calls. That middleware doesn't match these routes, so right now I wouldn't consider it.
Do you have to maybe, start at a specific page and navigate from there, for this to happen?
from next.js.
Okay, thank you. I will create a sample of the code used in my project tomorrow for your reference. @icyJoseph
from next.js.
I have one more question: does using shallow navigation cause getServerSideProps not to trigger? @icyJoseph
from next.js.
I have one more question: does using shallow navigation cause getServerSideProps not to trigger? @icyJoseph
It does, but it is subtle... Consider this, https://nextjs.org/docs/pages/building-your-application/routing/linking-and-navigating#shallow-routing:
import { useEffect } from 'react'
import { useRouter } from 'next/router'
// Current URL is '/'
function Page() {
const router = useRouter()
useEffect(() => {
// Always do navigations after the first render
router.push('/?counter=10', undefined, { shallow: true })
}, [])
useEffect(() => {
// The counter changed!
}, [router.query.counter])
}
export default Page
Note that, we are at /
, and we change only the query, with shallow true, and that skips remounting the page. However, if we change page (actual page file), then it doesn't matter if we do shallow navigation, the page unmounts.
Go to, https://stackblitz.com/edit/nextjs-bqzsj3?file=pages%2Fgssp%2F[slug].tsx and click the Go to example
text
Now you see a timestamp, and a shallow and unshallow pair of links. Clicking the shallow link, doesn't change the timestamp, but clicking the unshallow link does...
from next.js.
Actually, there is a slight difference: I only use router.replace(url, undefined, { shallow: true })
when changing certain parameters in the address bar. But for page navigation, I don't use shallow: true
.
Page: /category/[...url].tsx
The general flow is:
/categories/women/ready-to-wear.html
- Use
router.replace(url, undefined, { shallow: true })
to navigate to/categories/women/ready-to-wear.html?page=2
- Use
router.push('/categories/women.html')
to navigate to/categories/women.html
- Use
router.replace(url, undefined, { shallow: true })
to navigate to/categories/women.html?page=2
Then, when clicking the browser's forward and back buttons to switch between /categories/women/ready-to-wear.html?page=2
and /categories/women.html?page=2
, the getServerSideProps
sometimes doesn't trigger.
from next.js.
I wonder if this is a bug on 13.5.6? or if shallow + middleware, is causing some odd behavior with some of your pages 🤔
When shallow routing is used with middleware it will not ensure the new page matches the current page like previously done without middleware. This is due to middleware being able to rewrite dynamically and can't be verified client-side without a data fetch which is skipped with shallow, so a shallow route change must always be treated as shallow.
But I am just guessing at this point, I can't reproduce the issue though (on your page) - how do you do step 3?
from next.js.
Ah, I finally managed to reproduce the issue...
from next.js.
This is just a shot in the dark, but, could you use the object URL form? https://nextjs.org/docs/pages/api-reference/functions/use-router#with-url-object
I suspect the url
part is getting dropped in the history of the page. I don't want to write a long winded explanation, for something that might be wrong, but could you give it a go?
You'd need to specify the url
as a query, as well as the page query param, and the pathname would be the actual file path.
from next.js.
Okay, I will replace router.push('/categories/women.html')
with router.push({ pathname: '/category/[...url]', query: { url: ['women.html'] } })
as you suggested.
Example:
Page: /category/[...url].tsx
/categories/women/ready-to-wear.html
- Use
router.replace(url, undefined, { shallow: true })
to navigate to/categories/women/ready-to-wear.html?page=2
- Use
router.push({ pathname: '/category/[...url]', query: { url: ['women.html'] } })
to navigate to/categories/women.html
- Use
router.replace(url, undefined, { shallow: true })
to navigate to/categories/women.html?page=2
Thank you. @icyJoseph
from next.js.
After testing with the above code flow, the issue still exists. @icyJoseph
from next.js.
I meant to also change the replace calls, those are the problem I think... I also wonder if you need to pass an actual value, rather than undefined
into the calls 😅 It has meaning...
from next.js.
In router.replace(url, undefined, { shallow: true })
, the undefined
parameter is essentially a placeholder indicating that we do not wish to pass a value for the second argument. We only want to utilize the { shallow: true }
option. In our scenario, we want the product listing page to display the page number in the address bar, so we include the page number in the URL. This is the only use case we have for this feature.
from next.js.
Sort of, not quite, I know what you are thinking of, and yes that's a valid pattern they allow, but that second parameter has meaning.
In the Link component, that's the as
prop, but in the router methods, it is the second parameter.
router.push(url, as, options) // same for replace
as: UrlObject | String - Optional decorator for the path that will be shown in the browser URL bar. Before Next.js 9.5.3 this was used for dynamic routes.
So, I'd like you to use the as
value as well, which would be, what the browser URL should show, and the first argument, the file path /[...slug]
. Make sure to get it working correctly, it has been a while since I used these.
The reason why I think something fishy is going on there, is the history state I see saved. Before page 2:
{
as: "/categories/women/ready-to-wear.html",
key: "0037cbki",
options: {locale: undefined, _shouldResolveHref: true},
url: "/categories/[...url]?url=women&url=ready-to-wear.html"
}
After:
{
as: "/categories/women/ready-to-wear.html?page=2",
key: "0037cbki",
options: {scroll: false, shallow: true, _shouldResolveHref: true},
url: "/categories/[...url]?page=2",
__N: true
}
from next.js.
Got it, thank you for your feedback. I will replace the as
parameter in router.push(url, as, options)
with the actual value instead of undefined
. Then, I will proceed with testing to see if this resolves the issue.
from next.js.
Hi, after testing your suggestions, I still encounter the same issue. Are there any other solutions?
Here is an example: https://github.com/rick-liruixin/next-getServerSideProps-bug-demo/blob/main/pages/categories/%5B...url%5D.jsx
If possible, could you test it on my demo? @icyJoseph
from next.js.
Hello, is there any scheduled time for this fix? Please inform me under this issue once the fix is applied in the new version. @icyJoseph
from next.js.
Hi, sorry I just got back from a long weekend. I'll look into your repository now.
from next.js.
Hello, how's the progress going? @icyJoseph
from next.js.
Related Issues (20)
- ScrollAndFocusHandler incompatibility with AG Grid
- Docs: On Firefox, with dom.enable_performance_observer set to false, https://nextjs.org/ crashes
- Docs: location of cached images is wrong
- Third-party script synchronous loading Hydration problem
- Inconsistent handling of notFound() from server action
- Importing Next.js Link component in Route Handler results in "Could not parse module"
- Critical bug in NextJS v14.2.7 - renders page source code instead of components HOT 1
- Next v14 Double Render HOT 1
- Turbopack: `resourceQuery` from import isn't passed to loaders HOT 1
- Docs: Documentation incorrectly says database calls will opt in to server side rendering by default
- Docs: geolocation is confusing (w/vercel) - why is req.geo only available on middlewere
- Error: headers was called outside a request scope, when using Authjs v5.0.0-beta.19 and next dev --turbo
- Import .ts files in Instrumentation HOT 1
- Duplicate className in deploy-button.tsx causing Tailwind CSS error in with-supabase example
- [SWC] [Webpack] [Tree Shaking] combines exports of modules and constants across pages HOT 1
- Unexpected behaviour of app router when deployed to vercel
- [Next/Jest] debugger statement is ignored in chrome debugger
- Code imported inside instrumentation.ts can't mutate imported modules
- @svgr/webpack integration crashes on render inside server component (undefined runtime error) HOT 1
- Favicon.ico link not using `sizes="any"`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from next.js.