Skip to content

Commit 9cb84c4

Browse files
authored
fix(live-preview): encode query string url (#7635)
## Description Fixes #7529 - [x] I have read and understand the [CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md) document in this repository. ## Type of change - [x] Bug fix (non-breaking change which fixes an issue) ## Checklist: - [x] I have added tests that prove my fix is effective or that my feature works - [x] Existing test suite passes locally with my changes
1 parent 390f888 commit 9cb84c4

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

packages/live-preview/src/mergeData.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ export const mergeData = async <T>(args: {
7171
try {
7272
res = await requestHandler({
7373
apiPath: apiRoute || '/api',
74-
endpoint: `${collection}?depth=${depth}&where[id][in]=${Array.from(ids).join(',')}`,
74+
endpoint: encodeURI(
75+
`${collection}?depth=${depth}&where[id][in]=${Array.from(ids).join(',')}`,
76+
),
7577
serverURL,
7678
}).then((res) => res.json())
7779

test/live-preview/int.spec.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ describe('Collections - Live Preview', () => {
3131
let serverURL
3232

3333
let testPost: Post
34+
let testPostTwo: Post
3435
let tenant: Tenant
3536
let media: Media
3637

@@ -54,6 +55,15 @@ describe('Collections - Live Preview', () => {
5455
},
5556
})
5657

58+
testPostTwo = await payload.create({
59+
collection: postsSlug,
60+
data: {
61+
slug: 'post-2',
62+
title: 'Test Post 2',
63+
tenant: tenant.id,
64+
},
65+
})
66+
5767
// Create image
5868
const filePath = path.resolve(dirname, './seed/image-1.jpg')
5969
const file = await getFileByPath(filePath)
@@ -1280,4 +1290,85 @@ describe('Collections - Live Preview', () => {
12801290
expect(merge3.layout).toHaveLength(0)
12811291
expect(merge3._numberOfRequests).toEqual(0)
12821292
})
1293+
1294+
it('properly encodes URLs in requests', async () => {
1295+
const initialData: Partial<Page> = {
1296+
title: 'Test Page',
1297+
}
1298+
1299+
let capturedEndpoint: string | undefined
1300+
1301+
const customRequestHandler = async ({ apiPath, endpoint, serverURL }) => {
1302+
capturedEndpoint = `${serverURL}${apiPath}/${endpoint}`
1303+
1304+
const mockResponse = {
1305+
ok: true,
1306+
status: 200,
1307+
headers: new Headers({ 'Content-Type': 'application/json' }),
1308+
json: () => ({
1309+
docs: [
1310+
{
1311+
id: testPost.id,
1312+
slug: 'post-1',
1313+
tenant: { id: 'tenant-id', title: 'Tenant 1' },
1314+
title: 'Test Post',
1315+
createdAt: new Date().toISOString(),
1316+
updatedAt: new Date().toISOString(),
1317+
},
1318+
{
1319+
id: testPostTwo.id,
1320+
slug: 'post-2',
1321+
tenant: { id: 'tenant-id', title: 'Tenant 1' },
1322+
title: 'Test Post 2',
1323+
createdAt: new Date().toISOString(),
1324+
updatedAt: new Date().toISOString(),
1325+
},
1326+
],
1327+
}),
1328+
}
1329+
1330+
return Promise.resolve(mockResponse as unknown as Response)
1331+
}
1332+
1333+
const mergedData = await mergeData({
1334+
depth: 1,
1335+
fieldSchema: schemaJSON,
1336+
incomingData: {
1337+
...initialData,
1338+
relationshipPolyHasMany: [
1339+
{ value: testPost.id, relationTo: postsSlug },
1340+
{ value: testPostTwo.id, relationTo: postsSlug },
1341+
],
1342+
},
1343+
initialData,
1344+
serverURL,
1345+
returnNumberOfRequests: true,
1346+
collectionPopulationRequestHandler: customRequestHandler,
1347+
})
1348+
1349+
expect(mergedData.relationshipPolyHasMany).toMatchObject([
1350+
{
1351+
value: {
1352+
id: testPost.id,
1353+
slug: 'post-1',
1354+
title: 'Test Post',
1355+
},
1356+
relationTo: postsSlug,
1357+
},
1358+
{
1359+
value: {
1360+
id: testPostTwo.id,
1361+
slug: 'post-2',
1362+
title: 'Test Post 2',
1363+
},
1364+
relationTo: postsSlug,
1365+
},
1366+
])
1367+
1368+
// Verify that the request was made to the properly encoded URL
1369+
// Without encodeURI wrapper the request URL - would receive string: "undefined/api/posts?depth=1&where[id][in]=66ba7ab6a60a945d10c8b976,66ba7ab6a60a945d10c8b979
1370+
expect(capturedEndpoint).toContain(
1371+
encodeURI(`posts?depth=1&where[id][in]=${testPost.id},${testPostTwo.id}`),
1372+
)
1373+
})
12831374
})

0 commit comments

Comments
 (0)