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

Product Pages: Support encoded slashes #2163

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions frontend/templates/product-list/server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,23 @@ async function getSafeServerState({
function getDevicePathSegments(
context: GetServerSidePropsContext<ParsedUrlQuery>
) {
const { deviceHandleItemType } = context.params || {};
if (Array.isArray(deviceHandleItemType)) {
return deviceHandleItemType;
}
return [];
/**
* We should be able to use `context.params` here,
* but see: https://github.com/vercel/next.js/issues/49646
*
* Basically something like device:variant%2FWithASlash
* Will be parsed into:
* ['device', 'variant', 'WithASlash']
* Instead of:
* ['device', 'variant/WithASlash']
*/
const path = context.resolvedUrl.split('?')[0];
Copy link
Member

Choose a reason for hiding this comment

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

I forget which is which, but will this be the right url when the client side is hitting the next.js data api to just get the props?

I'm not entirely sure we'll run into instances where that will happen for this case, but it seems feasible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://nextjs.org/docs/pages/api-reference/functions/get-server-side-props#context-parameter

resolvedUrl

A normalized version of the request URL that strips the _next/data prefix for client transitions and includes original query values.

The docs don't indicate much clarity here, but it seemed safe enough to me.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds like the right one!

// turn into an array of path segments
const segments = path.split('/').filter(Boolean);
// remove the first segment, which is always 'Parts'
segments.shift();
const decodedSegments = segments.map(decodeURIComponent);
return decodedSegments;
}

function getDeviceCanonicalPath(
Expand Down