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

Rework query parsing #2396

Merged
merged 8 commits into from Mar 27, 2024
Merged

Rework query parsing #2396

merged 8 commits into from Mar 27, 2024

Conversation

matc-pub
Copy link
Contributor

Description

Passes the result of the getQueryParams implemention for a component, to the component as props and to its fetchInitialData as query. This makes server and client use the same methods to parse query parameters. Also passes the result of matchPath as match to fetchInitialData , this is the same as props.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..".

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.
Copy link
Member

@dessalines dessalines left a 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);
Copy link
Member

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.

@matc-pub
Copy link
Contributor Author

Did you test this? It seems to completely break everything but the initial page loads.

I tested it with a downgraded version of lemmy-js-client.

-    "lemmy-js-client": "0.19.4-alpha.4",
+    "lemmy-js-client": "0.19.4-alpha.2",

This downgrades cross-fetch. Without this I get 'fetch' called on an object that does not implement interface Window. when trying to login.

@dessalines
Copy link
Member

We removed the dependency on cross-fetch in this one.

I created a deploy with that included, could you try this version out?

0.19.4-alpha.14

Comment on lines 67 to 80
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
}
}
Copy link
Member

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 ifs instead of just checking getQueryParams && this.isoData.site_res?

Copy link
Contributor Author

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,
Copy link
Member

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,
Copy link
Member

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.

Suggested change
fallbacks: FallbacksT,
fallbacks: FallbacksT = {},

type Empty = NonNullable<unknown>;

type QueryMapping<PropsT, FallbacksT extends Empty> = {
[K in keyof PropsT]-?: (
Copy link
Member

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.

Copy link
Contributor Author

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];
};

@matc-pub
Copy link
Contributor Author

0.19.4-alpha.14 works for me. Also tested with the Dockerfile. 0.19.4-alpha.14 and npm update also works fine.

@dessalines
Copy link
Member

Yep, that new lemmy-js-client dep does work correctly, could you also include that too.

@matc-pub
Copy link
Contributor Author

#2398 is this pr merged with #2397, for lemmy-js-client 0.19.4-alpha.14

Copy link
Member

@dessalines dessalines left a 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!

Copy link
Member

@SleeplessOne1917 SleeplessOne1917 left a 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.

@matc-pub
Copy link
Contributor Author

Once the default arg change is made I'll merge.

Is already part of the Small getQueryParams cleanup commit

@SleeplessOne1917 SleeplessOne1917 merged commit 70e382b into LemmyNet:main Mar 27, 2024
1 check passed
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.

None yet

3 participants