Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fifty-rivers-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/hydrogen': patch
---

Fix the scroll restoration on page transitions
2 changes: 1 addition & 1 deletion packages/hydrogen/src/components/Link/tests/Link.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('<Link />', () => {

setTimeout(() => {
try {
expect(setServerProps).toHaveBeenCalledWith({
expect(setServerProps.mock.calls[0][0]()).toStrictEqual({
pathname: '/products/hydrogen',
search: '',
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import React, {
// @ts-ignore
useTransition,
useState,
useEffect,
} from 'react';

declare global {
Expand Down Expand Up @@ -86,16 +85,22 @@ export function ServerPropsProvider({

const setServerPropsCallback = useCallback<ServerPropsSetter>(
(input, propValue) => {
setServerProps((prev) => getNewValue(prev, input, propValue));
startTransition(() => {
setServerProps((prev) => getNewValue(prev, input, propValue));
setServerPropsForRsc((prev) => getNewValue(prev, input, propValue));
});
},
[]
);

const setLocationServerPropsCallback = useCallback<ServerPropsSetter>(
(input, propValue) => {
// Flush the existing user server state when location changes, leaving only the persisted state
setServerProps({});
setLocationServerProps((prev) => getNewValue(prev, input, propValue));
startTransition(() => {
setServerPropsForRsc((prev) => getNewValue(prev, input, propValue));
setServerProps({});
setLocationServerProps((prev) => getNewValue(prev, input, propValue));
});
},
[]
);
Expand Down Expand Up @@ -138,39 +143,19 @@ export function ServerPropsProvider({
};
}

const resolvedServerPropsForRsc = useMemo(() => {
return {
...serverProps,
...locationServerProps,
};
}, [serverProps, locationServerProps]);

const resolvedServerProps = useMemo(() => {
return {
...serverProps,
};
}, [serverProps]);

useEffect(() => {
startTransition(() => {
setServerPropsForRsc(resolvedServerPropsForRsc);
});
return () => {};
}, [resolvedServerPropsForRsc]);

const value = useMemo(
() => ({
pending,
locationServerProps: locationServerProps,
serverProps: resolvedServerProps,
serverProps,
setServerProps: setServerPropsCallback,
setLocationServerProps: setLocationServerPropsCallback,
getProposedLocationServerProps: getProposedLocationServerPropsCallback,
}),
[
pending,
locationServerProps,
resolvedServerProps,
serverProps,
setServerPropsCallback,
setLocationServerPropsCallback,
getProposedLocationServerPropsCallback,
Expand Down