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

[router] fix initial url on web when using baseUrl #27287

Merged
merged 7 commits into from Mar 12, 2024

Conversation

marklawlor
Copy link
Contributor

Why

ENG-11108

When using a baseUrl on web after the initial load a history.replace event is fired which strips the baseUrl. This is caused by React Navigation examining the current URL (which has the baseURL) and the url attribute on the current focused route (which does not include the baseURL). As they are different, it assumes the URL is wrong and changes it via history.replace.

How

I tried to fix this within the existing code, but eventually concluded that we simply need to fork useLInking for web as well. This fork simply removes the logic to use the URL on the state, and fall back to getPathFromState (which correctly handles baseUrl)

Test Plan

Checklist

Copy link

linear bot commented Feb 26, 2024

@expo-bot expo-bot added the bot: suggestions ExpoBot has some suggestions label Feb 26, 2024
@expo-bot expo-bot added bot: passed checks ExpoBot has nothing to complain about and removed bot: suggestions ExpoBot has some suggestions labels Feb 26, 2024
@marklawlor marklawlor marked this pull request as ready for review February 26, 2024 10:13
@marklawlor
Copy link
Contributor Author

@@ -1,3 +1,405 @@
import useLinking from '@react-navigation/native/lib/module/useLinking';
/* eslint-disable */
Copy link
Contributor Author

Choose a reason for hiding this comment

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

All Forked sections have Start of fork/End of fork code comments

// ServerContext is used inside ServerContainer to set the location during SSR: https://github.com/react-navigation/react-navigation/blob/13d4aa270b301faf07960b4cd861ffc91e9b2c46/packages/native/src/ServerContainer.tsx#L50-L54
// Expo Router uses the `initialLocation` prop to set the initial location during SSR:
const location = React.useContext(ServerLocationContext);
const server = { location };
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@EvanBacon I'm not sure this behaviour is correct.

ServerContext is only used in React Navigation inside the ServerContainer to set the initial URL https://github.com/react-navigation/react-navigation/blob/13d4aa270b301faf07960b4cd861ffc91e9b2c46/packages/native/src/ServerContainer.tsx#L50-L54

But Expo Router never sets the location during renderToStaticContent

<ServerContainer ref={ref}>{element}</ServerContainer>

If I were to replicate this current behaviour exactly, I should just remove ServerContext. Instead I've replaced it with a new context (the Provider sets the location in ExpoRoot)

@marklawlor
Copy link
Contributor Author

Note: Test without JavaScript enabled

@marklawlor marklawlor merged commit 5a4eb49 into main Mar 12, 2024
10 checks passed
@marklawlor marklawlor deleted the marklawlor/router/ENG-11108 branch March 12, 2024 21:25
marklawlor added a commit that referenced this pull request Mar 18, 2024
…xt (#27712)

# Why

This should have been part of #27287

Forks the `NavigationContainer` to use the custom `useLinking` which
appends the baseURL to the redirect that React Navigation performs when
current state != focused state

# Test Plan

Tested using @byCedric's repo
https://github.com/byCedric/expo-issue-router-base-url

---------

Co-authored-by: Expo Bot <34669131+expo-bot@users.noreply.github.com>
@brentvatne brentvatne added the published Changes from the PR have been published to npm label Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot: fingerprint compatible bot: passed checks ExpoBot has nothing to complain about published Changes from the PR have been published to npm
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants