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

fix: URL instance methods work in load #5183

Conversation

elliott-with-the-longest-name-on-github
Copy link
Contributor

Fixes #5083.

This is the "surgical approach" -- just replace the hash property with a custom getter. The other option is (if we're really committed to using a Proxy):

new Proxy(url, {
    get: (target, property, receiver) => {
        if (property === 'hash') {
            throw new Error(
                'url.hash is inaccessible from load. Consider accessing hash from the page store within the script tag of your component.'
            );
        }

        if (!(property in target)) {
          return undefined;
        }
        const value = target[property];
        return typeof value === "function"
          ? (...args) => value.apply(target, args)
          : value;
    }
});

This scares me a little bit, because it's a lot harder to anticipate what other side effects it might exhibit.

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. All changesets should be patch until SvelteKit 1.0

@changeset-bot
Copy link

changeset-bot bot commented Jun 22, 2022

🦋 Changeset detected

Latest commit: 03b19b3

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

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

@Rich-Harris
Copy link
Member

Thank you — this is definitely the right approach, rather than making the proxy more sophisticated — confirmed at excruciating length via Twitter.

I made a couple of small tweaks:

  • using a LoadURL subclass instead of monkey patching — feels less naughty, somehow
  • if someone wasn't using destructuring, we'd want event.url === event.url to be true, so create the LoadURL instance eagerly and return it from the getter
  • use it for SSR as well as client-side navigation, so that the error is consistent

It occurred to me that we probably want to do something similar for the prerendering proxy (the one that blocks use of url.search[Params]), but I left that out for now just to make life easy

@elliott-with-the-longest-name-on-github
Copy link
Contributor Author

Ugh, thanks for that Twitter thread. After reading a few dozen subthreads, I think I'm starting to understand why the proxy approach doesn't work: It's because JavaScript is stupid. 🤣

If we want to implement similar behavior for prerendering, I'm happy to tackle that. I can do it as part of this PR or as a separate one after this one is merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can't location.assign(url) in load() function: throws Illegal invocation
2 participants