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

Client-side routing for <form method="GET"> #7828

Merged
merged 16 commits into from Nov 30, 2022
Merged

Client-side routing for <form method="GET"> #7828

merged 16 commits into from Nov 30, 2022

Conversation

Rich-Harris
Copy link
Member

@Rich-Harris Rich-Harris commented Nov 27, 2022

Closes #7251. Just a failing test for now, because we need to refactor the find_anchor stuff to make this work, and there's no point embarking on that until #7776 is merged.

TODO:

  • add a submit handler (roughly described here, except it needs an event.defaultPrevented check)
  • add a form navigation type (or submit? not sure), that is applied regardless of willUnload
  • docs

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 Nov 27, 2022

🦋 Changeset detected

Latest commit: 2536be1

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

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit 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

Copy link
Contributor

@tcc-sejohnson tcc-sejohnson left a comment

Choose a reason for hiding this comment

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

Also opening another (very small) PR against this for type improvements

packages/kit/src/runtime/client/client.js Outdated Show resolved Hide resolved
packages/kit/src/runtime/client/client.js Outdated Show resolved Hide resolved
packages/kit/src/runtime/client/client.js Outdated Show resolved Hide resolved
if (is_external_url(url, base)) return;

const { noscroll, reload } = get_router_options(
/** @type {HTMLFormElement} */ (event.target)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/** @type {HTMLFormElement} */ (event.target)
/** @type {HTMLFormElement | null} */ (event.target)

again, probably shouldn't pretend nullable things aren't possibly null and should just handle null in the function or return early

Copy link
Member Author

Choose a reason for hiding this comment

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

this is just TypeScript being dumb though, no? Could event.target ever actually be null?

Copy link
Member

Choose a reason for hiding this comment

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

It's not null in this case though, we know it exists. The types don't, that's why they play it safe.

packages/kit/src/runtime/client/client.js Outdated Show resolved Hide resolved
@tcc-sejohnson
Copy link
Contributor

Opened #7883 for smol type improvements

Co-authored-by: S. Elliott Johnson <sejohnson@torchcloudconsulting.com>
Co-authored-by: S. Elliott Johnson <sejohnson@torchcloudconsulting.com>
Co-authored-by: Geoff Rich <4992896+geoffrich@users.noreply.github.com>
Comment on lines 98 to 106
export function find_anchor(element, target) {
while (element !== target) {
if (element.nodeName.toUpperCase() === 'A') {
return /** @type {HTMLAnchorElement | SVGAElement} */ (element);
}

element = /** @type {Element} */ (parent_element(element));
}
}
Copy link
Contributor

@PatrickG PatrickG Nov 29, 2022

Choose a reason for hiding this comment

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

Suggested change
export function find_anchor(element, target) {
while (element !== target) {
if (element.nodeName.toUpperCase() === 'A') {
return /** @type {HTMLAnchorElement | SVGAElement} */ (element);
}
element = /** @type {Element} */ (parent_element(element));
}
}
export function find_anchor(element, target) {
if (element.nodeName.toUpperCase() !== 'A') {
element = element.closest('a');
}
if (element && target.contains(element)) {
return /** @type {HTMLAnchorElement | SVGAElement} */ (element);
}
}

I'm not sure if this is considered premature optimization already, but I thought it might be worth a suggestion.
I'm not a benchmark expert, but I created this (probably unrealistic) benchmark which hints that closest + contains (which we need anyway) might be faster than the while loop:
https://measurethat.net/Benchmarks/Show/22320/0/find-anchor

Copy link
Member Author

Choose a reason for hiding this comment

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

I've no doubt that's true, but unfortunately closest() doesn't work, because it can't find elements in parent shadow DOM — yet another reason adding it to the web platform was a mistake

Copy link
Member Author

Choose a reason for hiding this comment

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

(incidentally, element.closest(selector) will return element if it matches selector, so if this did work — if we decided not to bother with shadow DOM, on the basis that the wounds are self-inflicted — we could dispense with the first if block)

Copy link
Contributor

@PatrickG PatrickG Nov 30, 2022

Choose a reason for hiding this comment

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

I've no doubt that's true, but unfortunately closest() doesn't work, because it can't find elements in parent shadow DOM — yet another reason adding it to the web platform was a mistake

Oh, makes sense. I didn't thought about that.

(incidentally, element.closest(selector) will return element if it matches selector, so if this did work — if we decided not to bother with shadow DOM, on the basis that the wounds are self-inflicted — we could dispense with the first if block)

I included this element.nodeName.toUpperCase() !== 'A' check because it was so much faster than .closest('a') on an HTMLAnchorElement in my tests.

Co-authored-by: Patrick <Patrick@ShowYou.us>
const options = get_router_options(a);
if (options.reload) continue;

if (external || options.reload) continue;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (external || options.reload) continue;

This is a remnant of my previous suggestion. Github didn't let me include this line in the selection because there was a deleted line in between.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks, we also need to delete some of the empty line or lint would fail, removed in a separate commit.

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.

enhance should progressively enhance <form method="GET">
7 participants