-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
fix: make injectQuery idempotent #14424
fix: make injectQuery idempotent #14424
Conversation
|
@bluwy Thanks for the feedback! I've made the changes you've asked for, let me know what you think. |
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.
Forgot to followup on this! Thanks for making the changes, I have one small nit below. Before merging this, I'd like another eye from others too.
Presumably this will have small perf implications, but I think it shouldn't be a lot. Any other way to handle duplicate params would be pretty hard.
Just ran a perf test and it seems like this PR makes it ~60% slower, which is not quite ideal 🤔 I implemented an alternative approach which failed one test case "path with injected query already present multiple times", but is still 30% slower. Snippet you can paste into perf.linkconst postfixRE = /[?#].*$/s
function cleanUrl(url) {
return url.replace(postfixRE, '')
}
const replacePercentageRE = /%/g
function injectQuery(url, queryToInject) {
const resolvedUrl = new URL(
url.replace(replacePercentageRE, '%25'),
'relative:///',
)
let { search, hash } = resolvedUrl
let pathname = cleanUrl(url)
const queryKey = queryToInject.split('=')[0]
if (search.includes(`?${queryKey}`) || search.includes(`&${queryKey}`)) {
search = search.replace(
new RegExp(`(\\?|&)${queryKey}=?.*?(&|$)`, 'g'),
(_, m1, m2) => (m2 ? m1 : ''),
)
}
return `${pathname}?${queryToInject}${search ? `&` + search.slice(1) : ''}${
hash ?? ''
}`
}
for (const [key, value] of Object.entries(data)) {
for (let i = 0; i < 100; i++) {
injectQuery(key, value)
}
} Maybe it's easier to fix the specific case you're encountering for now, since it's not often we get this issue. Something like: if (search.startsWith(`?${queryToInject}`)) return url Should be enough, and we can keep and skip the tests (that now fails) for future reference. There's still ~8% perf impact, but I think that's small enough. |
I think it would be ok to accept the performance hit to Maybe |
Hmm if that works I'm also ok with that. I thought there would be more to the MDX processing done, but so far I've only seen MDX always being treated as JS, so it should be fine. |
I think we can add mdx to the known types.
Is this by hand or by a |
I'm just coming back to this now that we've merged the unstable Vite support PR into Remix. I'm not sure what changed but I'm finding that the |
Description
This fixes the following issue with the
injectQuery
util:I ran into this because we're experimenting with using Vite for Remix. Remix injects a script tag in the HTML from the server to load all the JS modules for the current route before running the app, e.g.:
However, this fails in Vite when using MDX routes since the Vite dev server responds with the raw MDX content.
To fix this, I appended
?import
to the route module URLs in the Remix manifest in dev mode so that the Vite dev server responds with the compiled JS. This ensures that module URLs in the Remix manifest are valid JS modules, even when using other file formats. This means the injected script tag is now correct:However this causes issues when these URLs are imported within Remix library code because Vite transforms the import statement to use
injectQuery
which turns the query string into?import&import
.To fix this, I've updated
injectQuery
todetect whether thefirst delete any values forqueryToInject
argument is already present in the search params and that it has a blank value, and if so,queryToInject
from the search params to avoid adding it again.What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).