Skip to content
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

feat: support server redirects #491

Merged
merged 2 commits into from
Jan 28, 2025
Merged

Conversation

gbettencourt
Copy link

@gbettencourt gbettencourt commented Oct 23, 2024

Add the ability to redirect during SSR. Currently wouter only supports redirect once the app hydrates and the components render client side. Being able to redirect from the server is more efficient (only requires sending a status code/path back), resulting in a better user experience. The response can also be cached by the browser.

Resolves #468

Copy link

stackblitz bot commented Oct 23, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Copy link

codecov bot commented Oct 23, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (48b63a9) to head (5b79ba3).
Report is 10 commits behind head on v3.

Additional details and impacted files
@@            Coverage Diff            @@
##                v3      #491   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines            1         1           
=========================================
  Hits             1         1           
Flag Coverage Δ
?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@radfahrer
Copy link

@molefrog, we would greatly appreciate it if you could review this PR. We would very much like to migrate our application from react-router to wouter and this piece of missing functionality is holding us up.

Copy link
Owner

@molefrog molefrog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey! I am so sorry for my long reply. I've added a few comments, could you take a look?

README.md Show resolved Hide resolved
packages/wouter/src/index.js Show resolved Hide resolved
Comment on lines 321 to 324
const { to, href = to } = props;
const [, navigate] = useLocation();
const redirect = useEvent(() => navigate(to || href, props));
const { ssrContext } = useRouter();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an internal helper hook useLocationFromRouter that you can use combined with useRouter() to avoid extra context calls.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand this suggestion, there's no need to use location (the result of useLocationFromRouter) in this instance.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh never-mind, I see. I can optimize the existing call to useLocation above since I have a router ref. I pushed the changes.

@gbettencourt gbettencourt requested a review from molefrog January 27, 2025 22:36
@molefrog molefrog merged commit c0d37bf into molefrog:v3 Jan 28, 2025
4 checks passed
@molefrog
Copy link
Owner

Released in v3.5.0
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use wouter in SSR mode and detect redirects
3 participants