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

Do not scroll to top if the URL pathname has not changed #813

Merged
merged 6 commits into from
Mar 8, 2022
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/cool-swans-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/hydrogen': patch
---

Do not scroll to top if the URL pathname has not changed.
6 changes: 6 additions & 0 deletions .changeset/twenty-moose-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@shopify/hydrogen': patch
---

Remove Router client-only logic from server bundle and avoid extra waterfall requests during Hydration.
Extract part of the client bundle into separate modules that can be loaded in parallel.
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import {Fragment, useEffect, useState} from 'react';
import {Fragment, useEffect} from 'react';
import {Link} from '@shopify/hydrogen/client';
import {FocusTrap} from '@headlessui/react';

import MobileCountrySelector from './MobileCountrySelector.client';
import OpenIcon from './OpenIcon';

let scrollPosition = 0;

/**
* A client component that defines the navigation for a mobile storefront
*/
export default function MobileNavigation({collections, isOpen, setIsOpen}) {
const OpenFocusTrap = isOpen ? FocusTrap : Fragment;

const [topScrollOffset, setTopScrollOffset] = useState(0);

useEffect(() => {
if (isOpen) {
setTopScrollOffset(window.scrollY);
scrollPosition = window.scrollY;
document.body.style.position = 'fixed';
} else {
} else if (document.body.style.position) {
document.body.style.position = '';
window.scrollTo(0, parseInt(topScrollOffset, 10));
window.scrollTo(0, scrollPosition);
}
}, [isOpen, topScrollOffset]);
}, [isOpen]);

return (
<div className="lg:hidden">
Expand Down
2 changes: 1 addition & 1 deletion packages/hydrogen/src/components/Link/Link.client.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, {useCallback} from 'react';
import {useRouter} from '../Router';
import {useRouter} from '../../foundation/Router/Router';
import {createPath} from 'history';
import {useNavigate} from '../../hooks/useNavigate';

Expand Down
5 changes: 0 additions & 5 deletions packages/hydrogen/src/components/Router/index.tsx

This file was deleted.

4 changes: 2 additions & 2 deletions packages/hydrogen/src/entry-client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {hydrateRoot} from 'react-dom';
import type {ClientHandler} from './types';
import {ErrorBoundary} from 'react-error-boundary';
import {useServerResponse} from './framework/Hydration/rsc';
import {ServerStateProvider} from './client';
import {Router} from './foundation/Router/Router.client';
import {ServerStateProvider} from './foundation/ServerStateProvider';
import {Router} from './foundation/Router/Router';

const renderHydrogen: ClientHandler = async (ClientWrapper, config) => {
const root = document.getElementById('root');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type RouterContextValue = {

const RouterContext = createContext<RouterContextValue | {}>({});

let currentPath = '';

export const Router: FC<{history?: BrowserHistory}> = ({
history: pHistory,
children,
Expand All @@ -25,15 +27,17 @@ export const Router: FC<{history?: BrowserHistory}> = ({
const [firstLoad, setFirstLoad] = useState(true);
const [location, setLocation] = useState(history.location);

const {pending, setServerState} = useServerState();
const {pending, serverState, setServerState} = useServerState();

useEffect(() => {
// The app has just loaded
if (firstLoad) setFirstLoad(false);
// A navigation event has just happened
else if (!pending) {
else if (!pending && currentPath !== serverState.pathname) {
window.scrollTo(0, 0);
}

currentPath = serverState.pathname;
}, [pending]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the effect dependency include serverState.pathname?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As far as I understand, pending already changes every time the server state is updated since the whole thing is a React transition. Do we still need to add serverState.pathname in that case? 🤔


useEffect(() => {
Expand Down
10 changes: 2 additions & 8 deletions packages/hydrogen/src/foundation/ServerStateProvider/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
export {
ServerStateProvider,
ServerStateContext,
} from './ServerStateProvider.client';
export type {
ServerState,
ServerStateContextValue,
} from './ServerStateProvider.client';
export {ServerStateProvider, ServerStateContext} from './ServerStateProvider';
export type {ServerState, ServerStateContextValue} from './ServerStateProvider';
2 changes: 1 addition & 1 deletion packages/hydrogen/src/hooks/useNavigate/useNavigate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {useRouter} from '../../foundation/Router/Router.client';
import {useRouter} from '../../foundation/Router/Router';

type NavigationOptions = {
/** Whether to update the state object or URL of the current history entry. Default to false */
Expand Down
2 changes: 1 addition & 1 deletion packages/hydrogen/src/utilities/tests/shopifyMount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {DEFAULT_LOCALE} from '../../foundation/constants';

import {ShopifyConfig} from '../../types';
import {ShopifyProvider} from '../../foundation/ShopifyProvider';
import {Router} from '../../components/Router';
import {Router} from '../../foundation/Router/Router';
import {ServerState, ServerStateProvider} from '../../foundation';

type SetServerState = React.Dispatch<React.SetStateAction<ServerState>>;
Expand Down