Skip to content

Conversation

@brophdawg11
Copy link
Contributor

Adds a createStaticHandler method for the router usage on the server. This exposes 2 methods:

  • Create a router:
    • let { dataRoutes, query, queryRoute } = createStaticHandler({ routes })
    • dataRoutes is the routes with guaranteed unique IDs on them, and is required to be passed to <DataStaticRouter>
  • Handle a document request:
    • query(Request) => Promise<StaticRouterState | Response>
    • If a Response is returned - it should be handled as an HTTP response
    • If state is returned that can be rendered via <DataStaticRouter dataRoutes={dataRoutes} {state={state}>
  • Handle a route/data request:
    • queryRoute(Request, routeId) => Promise<any>
    • If a Response is returned - it should be handled as an HTTP response
    • If anything else is returned it should be sent back verbatim

This also fixes a small DataBrowserRouter hydration error due to a missing param for useSyncExternalStoreShim

Replaces #8986

@changeset-bot
Copy link

changeset-bot bot commented Jun 23, 2022

🦋 Changeset detected

Latest commit: a037379

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 5 packages
Name Type
react-router Patch
react-router-dom Patch
@remix-run/router Patch
react-router-dom-v5-compat Patch
react-router-native Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

() => router.state,
// We have to provide this so React@18 doesn't complain during hydration,
// but we pass our serialized hydration data into the router so state here
// is already synced with what the server saw
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This fixes the current hydration bug (#8972)

navigator={navigator}
>
<DataRoutes routes={routes} children={children} />
<DataRoutes routes={router.routes} children={children} />
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ensure we are always passing through data routes

// Call action if we received an action submission
let pendingActionData: RouteData | null = null;
let pendingActionError: RouteData | null = null;
let request = createRequest(location, opts?.submission);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the big change to current router internals in here. In order to better align the server/browser use-cases we life Request creation way up from where it used to be. That way we're better able to accept an external Request ion the SSR case

// Note: OK to mutate this in-place since it's a scoped var inside
// handleAction and mutation will not impact the startNavigation matches
// variable that we use for revalidation
matches = matches.slice(0, -1);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is handled by getTargetMatch now

});
}

if (isRouteRequest) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For Route requests we just return the data here, don't proceed with loaders

Comment on lines +1608 to +1611
let routeData = [result.errors, result.actionData, result.loaderData].find(
(v) => v
);
let value = Object.values(routeData || {})[0];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Don't ever return state to a queryRoute call, and instead grab the raw data. Should think about the non-Response error flow here. When we pick off the "value" here we sort of lose visibility as to whether it was an error or not. If user's are always throwing Error instances they could check themselves. but what if they throw 'not an error';...?

result = await handler({
params: match.params,
request: createRequest(location, submission),
request,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the old request creation spot - way down at the point we call a loader/action

// without unwrapping
if (isRouteRequest) {
throw result;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Don't unwrap responses for queryRoute

// redirect
if (skipRedirects) {
throw result;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Don't process redirects for query

@brophdawg11
Copy link
Contributor Author

Closing in favor of a rebased branch on top of the brophdawg11/deferred branch

@brophdawg11 brophdawg11 deleted the brophdawg11/ssr2 branch June 30, 2022 18:56
brophdawg11 added a commit that referenced this pull request Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant