Skip to content

Commit

Permalink
fix(example): handle redirects in TanStack router (#33)
Browse files Browse the repository at this point in the history
* fix(example): handle redirects in TanStack router

* fix: preserve query params in tanstack redirect

* cleanup: remove broken redirect note and add server/client redirect links
  • Loading branch information
drew-harris authored May 23, 2024
1 parent a7440f1 commit ecd5d6c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
12 changes: 8 additions & 4 deletions examples/tanstack-router-simple/src/routes/redirect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ function RedirectComponent() {
well on first load when relevant.
</p>

<p>NOTE: the server side of things doesn't seem to be working atm. Could be an issue in tanstack router.</p>

<p>
<Link to="/redirect" search={{ doRedirect: true }}>
Redirect
<Link to="/redirect" search={{ doRedirect: true }} className="text-blue-700">
Client Side Redirect
</Link>
</p>

<p>
<a href="/redirect?doRedirect=true" className="text-blue-700">
Server Side Redirect
</a>
</p>
</div>
);
}
7 changes: 6 additions & 1 deletion examples/tanstack-router-simple/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ const server = new Hono()
try {
const { app, router } = await entry.render(c.req.raw);

// Handle redirects
if (router.state.redirect) {
return c.redirect(router.state.redirect.href);
}

const { stream, statusCode } = await renderToStream({
app: () => app,
req: c.req.raw,
Expand All @@ -39,7 +44,7 @@ const server = new Hono()

let status = statusCode();

// Handle redirects
// Handle 404 errors
if (router.hasNotFoundMatch() && status !== 500) status = 404;

return new Response(stream, { status, headers: { 'Content-Type': 'text/html' } });
Expand Down

0 comments on commit ecd5d6c

Please sign in to comment.