Coder Social home page Coder Social logo

Comments (6)

icyJoseph avatar icyJoseph commented on June 9, 2024

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.

404nnotfoundddd avatar 404nnotfoundddd commented on June 9, 2024

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.

icyJoseph avatar icyJoseph commented on June 9, 2024

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.

404nnotfoundddd avatar 404nnotfoundddd commented on June 9, 2024

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.

icyJoseph avatar icyJoseph commented on June 9, 2024

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.

404nnotfoundddd avatar 404nnotfoundddd commented on June 9, 2024

I see, thanks my problem is solved

from next.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.