-
Notifications
You must be signed in to change notification settings - Fork 340
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
Rework query parsing #2396
Rework query parsing #2396
Conversation
Adds an IRoutePropsWithFetch definition for routes with getQueryParams or fetchInitialData to cause compiler errors when the types no longer match.
Problem: A search for "%ab" produces a url with "%25ab". Refreshing the page results in URLSearchParams turning "%25ab" back into "%ab". decodeURIComponent() then complains about "%ab" being malformed. This removes decodeURIComponent() calls for query parameters and composes all query strings with getQueryString(), which now uses URLSearchParams. Query parsing already goes through getQueryParams() which also uses URLSearchParams.
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.
Did you test this? It seems to completely break everything but the initial page loads.
I agree with this overall change tho, its a much cleaner way of doing this.
const communityForm: GetCommunity = { | ||
name: communityName, | ||
}; | ||
|
||
const dataType = getDataTypeFromQuery(urlDataType); |
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.
Getting rid of these is much cleaner, thx.
I tested it with a downgraded version of - "lemmy-js-client": "0.19.4-alpha.4",
+ "lemmy-js-client": "0.19.4-alpha.2", This downgrades cross-fetch. Without this I get |
We removed the dependency on cross-fetch in this one. I created a deploy with that included, could you try this version out?
|
src/shared/components/app/app.tsx
Outdated
let queryProps = routeProps; | ||
if (getQueryParams) { | ||
if (this.isoData.site_res) { | ||
queryProps = { | ||
...queryProps, | ||
...getQueryParams( | ||
routeProps.location.search, | ||
this.isoData.site_res, | ||
), | ||
}; | ||
} else { | ||
// ErrorGuard will catch this | ||
} | ||
} |
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 the else
block is going to be empty, why nest the if
s instead of just checking getQueryParams && this.isoData.site_res
?
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 was intended to point out that a RouteComponent
with getQueryParams
will never see props without that don't contain the query parameters.
FallbacksT extends Empty = Empty, | ||
>( | ||
processors: QueryMapping<PropsT, FallbacksT>, | ||
source: string | undefined, |
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.
Will source?; string,
work here?
>( | ||
processors: QueryMapping<PropsT, FallbacksT>, | ||
source: string | undefined, | ||
fallbacks: FallbacksT, |
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.
A default value would be good here. I noticed that you pass an empty object as the third arg to this function practically everywhere you call it.
fallbacks: FallbacksT, | |
fallbacks: FallbacksT = {}, |
type Empty = NonNullable<unknown>; | ||
|
||
type QueryMapping<PropsT, FallbacksT extends Empty> = { | ||
[K in keyof PropsT]-?: ( |
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.
Why the -
before the ?
? I've never seen that syntax used in typescript before.
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.
Makes specifying a mapping for each property manadatory. Works like Required<T>
:
/**
* Make all properties in T required
*/
type Required<T> = {
[P in keyof T]-?: T[P];
};
|
Yep, that new lemmy-js-client dep does work correctly, could you also include that too. |
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.
Tests good for me, thx!
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.
Once the default arg change is made I'll merge.
Is already part of the Small getQueryParams cleanup commit |
Description
Passes the result of the
getQueryParams
implemention for a component, to the component asprops
and to itsfetchInitialData
asquery
. This makes server and client use the same methods to parse query parameters. Also passes the result ofmatchPath
asmatch
tofetchInitialData
, this is the same asprops.match
.As a result the server side rendering matches a lot closer the client side rendering. Previously the server only used the query parameters to fetch data, but ignored them during
renderToString
.Fixes an issue with double decoding query parameters, where the result of the first decoding can create malformed tokens for the second decoding. The fix relies on always using
URLSearchParams
to parse and create query strings.Fixes
<PictrsImage />
with an image like"../pictrs/image/...webp?some=thing"
trying to show thumbnails ending in"?some=thing?thumbnail.."
.