Replies: 5 comments 17 replies
-
|
Beta Was this translation helpful? Give feedback.
-
Yeah, that's something what I imagine.
There might be a way to improve it. |
Beta Was this translation helpful? Give feedback.
-
Basically, React doesn't recommend for server components to interact with |
Beta Was this translation helpful? Give feedback.
-
I realized my middleware needs to handle the |
Beta Was this translation helpful? Give feedback.
-
This seems to work: function redirectRSC(ctx: HandlerContext, path: string, component: string) {
ctx.res.headers = {
...ctx.res.headers,
// "Content-Type": "text/x-component",
"Content-Type": "text/plain; charset=utf-8",
};
const flightData = `2:{"name":"${component}","env":"Server","key":null,"owner":null}
1:D"$2"
0:{"${path.slice(1)}":"$L1"}
1:["$","meta",null,{"httpEquiv":"refresh","content":"0;url=/login"},"$2"]
`;
ctx.res.body = new ReadableStream({
start(controller) {
controller.enqueue(new TextEncoder().encode(flightData));
controller.close();
},
});
const accountMiddleware: Middleware = () => {
return async (ctx, next) => {
console.log("req path:", ctx.req.url.pathname);
if (
ctx.req.url.pathname.startsWith("/account")
|| ctx.req.url.pathname.startsWith("/account")
) {
const isRSC = ctx.req.url.pathname.startsWith("/RSC/");
const account = await getAccount(ctx);
if (!account) {
if (isRSC) {
// Send a meta refresh tag over RSC
redirectRSC(ctx, "/layout", "Layout");
return;
}
const resUrl = new URL(ctx.req.url.toString());
if (ctx.req.headers["x-forwarded-host"]) {
resUrl.hostname = ctx.req.headers["x-forwarded-host"];
}
resUrl.pathname = "/login";
c.redirect(resUrl.toString(), 302);
ctx.res.status = 302;
ctx.res.body = null;
return;
}
ctx.context.account = account;
}
}
}
export default accountMiddleware; I could not find a simple function to turn basic html element jsx into the flight format. I think this renderFragment might be what I'm looking for - but it isn't exported. https://github.com/facebook/react/blob/main/packages/react-server/src/ReactFlightServer.js#L1433 It's an interesting experiment... but it's probably better to just |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'd like an easy way to redirect from a server component or server
actionfunction. I'm not sure what React might already be working on in this area. Being able to return aResponse
object from a server component instead of JSX sounds like one way to handle it.I'd like my redirect to work with client-side navigation too... so if I navigate to a protected route, the RSC handler would be able to check - maybe with a suspense boundary to render a shell - and then redirect before any protected components are sent over the wire.
I can currently achieve a redirect in Waku by using middleware.
Roughly like this:
I found I had to set the redirect on the hono context and in the ctx.res to get it to work. I though it would work to just set
ctx.res.status
but it didn't.I saw
unstable_redirect()
exists inwaku/router/server
but I wasn't sure how to use it. Really I want something I can invoke from a server component or server function.Beta Was this translation helpful? Give feedback.
All reactions