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

Handle case when session storage is blocked #10848

Merged

Conversation

david-bezero
Copy link
Contributor

As discussed in #10842

This writes a descriptive warning to the console if sessionStorage is unavailable for any reason, but allows the scroll saving/resetting to continue working as much as possible (until the user leaves the page we still have the scroll positions in a global variable)

It also retro-fits a "happy-path" test for ScrollRestoration to confirm the scroll position is correctly reset and no warnings are printed.

Interestingly, writing the test turned up a behaviour quirk where the default getKey implementation fails to save the scroll position for the first page viewed (since its location.key is default on the first visit). I haven't attempted to fix this; I'm not even sure if it's a real issue outside the test environment.

@changeset-bot
Copy link

changeset-bot bot commented Sep 6, 2023

🦋 Changeset detected

Latest commit: b80680a

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

This PR includes changesets to release 4 packages
Name Type
react-router-dom Patch
react-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

@remix-cla-bot
Copy link
Contributor

remix-cla-bot bot commented Sep 6, 2023

Hi @david-bezero,

Welcome, and thank you for contributing to React Router!

Before we consider your pull request, we ask that you sign our Contributor License Agreement (CLA). We require this only once.

You may review the CLA and sign it by adding your name to contributors.yml.

Once the CLA is signed, the CLA Signed label will be added to the pull request.

If you have already signed the CLA and received this response in error, or if you have any questions, please contact us at hello@remix.run.

Thanks!

- The Remix team

@remix-cla-bot
Copy link
Contributor

remix-cla-bot bot commented Sep 7, 2023

Thank you for signing the Contributor License Agreement. Let's get this merged! 🥳

Comment on lines 1327 to 1334
} catch (error) {
console.warn("Failed to record scroll position in session storage. Scroll restoration will not work.", error);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

We have a built-in warning method we can use here:

Suggested change
} catch (error) {
console.warn("Failed to record scroll position in session storage. Scroll restoration will not work.", error);
}
} catch (error) {
warning(
false,
"Failed to save scroll positions in sessionStorage, <ScrollRestoration /> will not work properly."
);
console.error(error);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

why call console.error? I guess it's to make sure the cause is displayed somewhere, but since we can recover from the situation I don't think it should be reported as an error - could it remain console.warn?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah the idea there was to show the actual underlying error, but we could probably inline error.message into the warning as well if we wanted to keep just one console entry and avoid a console.error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added it using message (${error}). format, following an existing convention from safelyDecodeURI. This also avoids any issues if the thrown entity is not an object for whatever reason.

storageKey || SCROLL_RESTORATION_STORAGE_KEY,
JSON.stringify(savedScrollPositions)
);
window.history.scrollRestoration = "auto";
Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably keep this outside the try so it gets properly reset even if the sessonStorage set fails

@brophdawg11
Copy link
Contributor

I also just noticed this is pointed to main - do you mind rebasing and pointing to dev since this contains a source code change? And then would you also mind running yarn changeset to get a changeset added per #10848 (comment)?

@david-bezero david-bezero changed the base branch from main to dev September 13, 2023 15:16
@david-bezero
Copy link
Contributor Author

updated the logging, rebased, repointed, and added a changeset

.changeset/soft-forks-cough.md Outdated Show resolved Hide resolved
@brophdawg11 brophdawg11 merged commit f8194fd into remix-run:dev Sep 14, 2023
3 checks passed
@brophdawg11
Copy link
Contributor

Thanks for the PR!

@david-bezero david-bezero deleted the handle-session-storage-blocked branch September 14, 2023 14:23
@github-actions
Copy link
Contributor

🤖 Hello there,

We just published version 6.17.0-pre.0 which includes this pull request. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

@github-actions
Copy link
Contributor

🤖 Hello there,

We just published version 6.17.0 which includes this pull request. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

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.

None yet

2 participants