Skip to content

Commit

Permalink
Do not scroll to top if the URL pathname has not changed (#813)
Browse files Browse the repository at this point in the history
* Do not scroll to top if the URL pathname has not changed

* Refactor: initialize currentPath

* Fix mobile navigation resetting scroll

* Revert "Refactor: initialize currentPath"

This reverts commit c40d022.

* Remove router logic from server bundle and split client bundle in smaller chunks (#832)

* Change RouterClient to be a shared component to reduce server bundle size

* Change ServerStateProvider to be a shared component and avoid extra bundling in client build

* Update changeset
  • Loading branch information
frandiox authored Mar 8, 2022
1 parent 74b14e4 commit b1b959c
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 27 deletions.
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]);

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

0 comments on commit b1b959c

Please sign in to comment.