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
Conversation
🦋 Changeset detectedLatest commit: 2536be1 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
There was a problem hiding this 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
if (is_external_url(url, base)) return; | ||
|
||
const { noscroll, reload } = get_router_options( | ||
/** @type {HTMLFormElement} */ (event.target) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/** @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
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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.
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>
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)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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 returnelement
if it matchesselector
, 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 firstif
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
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.
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:
submit
handler (roughly described here, except it needs anevent.defaultPrevented
check)form
navigation type (orsubmit
? not sure), that is applied regardless ofwillUnload
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpm changeset
and following the prompts. All changesets should bepatch
until SvelteKit 1.0