-
Notifications
You must be signed in to change notification settings - Fork 27.7k
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
Soft navigated page on refresh shows raw data #48569
Comments
Please try to provide a simple reproduction repo/stackblitz so that we can try to help. |
For what it's worth, I've seen this issue as well but have not been able to create a simple reproduction, so I have refrained from opening an issue. |
@steve-marmalade i may have an idea, i got a similar issue when i was trying to use cloudfare cache with next app router, in cloudfare if you setup the cache to Next ISR (SSG) by default adds a corresponding cache-control to make it cacheable from the CDN, the only problem is that with app router, the same request to the same route is used whether your page is navigated via The problem here is that cloudfare cache (with the setup To fix that i just removed the It should be noted however that this will only happen if you use the app router, in next pages dir, next load the payload for link navigations with another request, something like this : GET /_next/data/q-1di4c_JMDaO2WkUSANX/path-to-your-page/your-page.json Cloudfare docs for cache rules : https://developers.cloudflare.com/cache/how-to/create-page-rules/#cache-everything |
To clarify App Router adds a few headers to the fetch when navigating client-side to get the RSC payload instead of HTML. Next.js on the server sets the Going to close this issue as the |
Thank you for the response, will check and come back |
Ok, after an intense testing, i found out that cloudfare CDN does not respect the to quote from Cloudfare docs :
To fix this i used a cloudfare worker to modify the request to the origin server by adding the vary headers as query params so that cloudfare would consider a request with export default {
async fetch(request, env, ctx) {
const url = new URL(request.url);
const varyKeys = [
'RSC',
'Next-Router-State-Tree',
'Next-Router-Prefetch',
'Accept-Encoding',
]
// append these headers to the searchParams in order to distinguish requests with them or not
for(const key of varyKeys) {
if (request.headers.has(key) ) {
url.searchParams.set(key, request.headers.get(key))
}
}
const rewrittenRequest = new Request(url, request);
let cache = true
// Do not cache routes starting with `/api`
if(url.pathname.startsWith('/api')) {
cache = false;
}
let response = await fetch(rewrittenRequest, {
cf: {
cacheEverything: cache,
// only cache 2xx status codes
cacheTtlByStatus: { 404: 1, "500-599": 0, "300-399": 1 }
},
});
// Reconstruct the Response object to make its headers mutable.
response = new Response(response.body, response);
return response;
},
}; And i also added a middleware to strip the import { NextResponse } from 'next/server';
import { NextRequest } from 'next/server';
export function middleware(request: NextRequest) {
const url = new URL(request.nextUrl);
// Check for vary headers appended by cloudfare worker
const varyKeys = [
'RSC',
'Next-Router-State-Tree',
'Next-Router-Prefetch',
'Accept-Encoding',
];
let varyFound = false;
// remove these headers from the URL
for (const key of varyKeys) {
if (url.searchParams.has(key)) {
url.searchParams.delete(key);
varyFound = true;
}
}
if (varyFound) {
return NextResponse.rewrite(url);
}
return NextResponse.next();
}
export const config = {
// exclude api, images & static paths (_next/static & _next/image)
matcher: [
'/((?!api|_next/static|_next/image|favicon.ico|robots.txt|sitemap.xml).*)',
],
};
|
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Verify canary release
Provide environment information
Which area(s) of Next.js are affected? (leave empty if unsure)
App directory (appDir: true), Routing (next/router, next/navigation, next/link)
Link to the code that reproduces this issue
To Reproduce
rec.mov
Describe the Bug
While navigating from one route to another, a soft navigation happens and the page loads correctly. On refreshing the just landed page, I get the data in:
format, and it is the same when i do a back/forward navigation.
Expected Behavior
Instead of showing the raw data, page should load the corresponding HTML
Which browser are you using? (if relevant)
Chrome 112.0.5615.49
How are you deploying your application? (if relevant)
No response
The text was updated successfully, but these errors were encountered: