Comments (6)
Is the issue here that, redirect actually answers back to the client. The browser then follows the redirect Location header, but, it doesn't include any other headers it receives from the server, when doing so. This is likely what's happening here.
To get pathnames in Server Components, if you can build them within the entry page.tsx, you can use the cache trickimplementation, and then have them available downstream.
from next.js.
Is the issue here that, redirect actually answers back to the client. The browser then follows the redirect Location header, but, it doesn't include any other headers it receives from the server, when doing so. This is likely what's happening here.
To get pathnames in Server Components, if you can build them within the entry page.tsx, you can use the cache
trickimplementation, and then have them available downstream.
Can you explain this more, I don't understand it "if you can build them within the entry page.tsx, you can use the cache trickimplementation, and then have them available downstream."
from next.js.
Like, you can make something lke:
// pseudo-code !-----
const createContainer = cache(() => {
const container = { current: null };
const getter = () => container.current;
const setter = (next) => { container.current = next };
return [getter, setter]
});
export const [getSomeData, setSomeData] = createContainer();
And now you can set some data, at page level for example, with the pathname, and then get it down in the SSR tree. Importantly, this only works during the SSR pass, and because it uses cache it is scope to the current request.
Here's an actual demo of it: https://stackblitz.com/edit/nextjs-q3dnp8?file=app%2Fpage.tsx,app%2FLocaleSwitch.tsx,app%2F[locale]%2Fpage.tsx,app%2Fserver-context.ts - using a Map instead
And a more long winded explanation: #63298 (comment)
And an NPM package that implements this nicely for you: https://www.npmjs.com/package/server-only-context
from next.js.
Like, you can make something lke:
// pseudo-code !----- const createContainer = cache(() => { const container = { current: null }; const getter = () => container.current; const setter = (next) => { container.current = next }; return [getter, setter] }); export const [getSomeData, setSomeData] = createContainer();And now you can set some data, at page level for example, with the pathname, and then get it down in the SSR tree. Importantly, this only works during the SSR pass, and because it uses cache it is scope to the current request.
Here's an actual demo of it: https://stackblitz.com/edit/nextjs-q3dnp8?file=app%2Fpage.tsx,app%2FLocaleSwitch.tsx,app%2F[locale]%2Fpage.tsx,app%2Fserver-context.ts - using a Map instead
And a more long winded explanation: #63298 (comment)
And an NPM package that implements this nicely for you: https://www.npmjs.com/package/server-only-context
I checked your reply and the npm package you mentioned and thanks it solved my prop drilling issue, but is the header not being included when using location/redirect the default behavior of the browser or is it a nexjts error? should the issue remain open
from next.js.
You can do a little experiment. Open the browser Network on DevTools, and check the preserve log option.
Then do the navigation that does your redirect.
Inspect the headers sent by the browser upon redirect, when it follows the location header.
Or you could just trust this Stack overflow answer, https://stackoverflow.com/a/1969728
from next.js.
I see, thanks my problem is solved
from next.js.
Related Issues (20)
- Inconsistent parsing of data URLs in CSS in Next 14 HOT 1
- intercepting routes not rendering under nested route HOT 3
- Can't use Knex in instrumentation hook HOT 3
- Tab title is reset for a moment when navigating to a dynamic page HOT 1
- Dev mode fail s with(WasmHash._updateWithBuffer ) HOT 1
- Parallel Routes cause redundant RSC fetches on navigation HOT 8
- When using `<Script>` with `crossorigin` specified, `crossorigin` is missing from `<link>` that preloads the script
- Layout doesn't cache and gets 1 server re-render for each broken image link
- Fill property for Next Image doesn't seem to work. HOT 6
- 404 when redirecting in server action with subdomain rewrite rule
- Build Error when trying to compile with 'use client' files.
- NextResponse.redirect is not working in middleware HOT 1
- Layout rerenders on same segment route change HOT 4
- Docs: Replace "twitter.com" to "x.com"
- Add option to enable Turbopack with `create-next-app` HOT 8
- Redirect URL not updating correctly in browser after successful redirect HOT 11
- NextUrl pathname return wrong HOT 1
- "edge" runtime is not allowed in pages router getServerSideProps. "experimental-edge" is forced though it shouldn't be. HOT 1
- Triggering notFound in generateMetadata results in Error: NEXT_NOT_FOUND when the delayed notFound call is used (e.g. fetch) HOT 1
- Next.js Compatibility Issues on Chrome 80: Style, useEffect, and Query Problems HOT 1
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.