Skip to content

Commit

Permalink
fix: getDefaultRedirectPath with locales
Browse files Browse the repository at this point in the history
  • Loading branch information
tobeycodes committed Aug 30, 2023
1 parent de501ff commit c1fa9da
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
29 changes: 29 additions & 0 deletions packages/next/src/handlers/__tests__/previewHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,35 @@ describe('previewHandler', () => {
expect(res._getStatusCode()).toBe(302);
});

it('sets preview cookie path with locale', async () => {
const { req, res } = createMocks({
method: 'GET',
query: {
post_id: DRAFT_POST_ID,
token: VALID_AUTH_TOKEN,
post_type: 'post',
locale: 'es',
},
});

res.setPreviewData = jest.fn();
await previewHandler(req, res);

expect(res.setPreviewData).toHaveBeenCalledWith(
{
authToken: 'this is a valid auth',
id: 57,
postType: 'post',
revision: false,
},
{
maxAge: 300,
path: '/es/modi-qui-dignissimos-sed-assumenda-sint-iusto-preview=true',
},
);
expect(res._getStatusCode()).toBe(302);
});

it('set preview cookie path to all paths if onRedirect is passed without getRedirectPath', async () => {
const { req, res } = createMocks({
method: 'GET',
Expand Down
8 changes: 2 additions & 6 deletions packages/next/src/handlers/previewHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,8 @@ export async function previewHandler(
const singleRoute = postTypeDef.single || '/';
const prefixRoute = singleRoute === '/' ? '' : singleRoute;
const slugOrId = revision ? post_id : slug || post_id;

if (locale) {
return `/${locale}/${prefixRoute}/${slugOrId}`;
}

return `${prefixRoute}/${slugOrId}`;
const path = [locale, prefixRoute, slugOrId].filter((n) => n).join('/');
return `/${path}`;
};

const redirectPath =
Expand Down

0 comments on commit c1fa9da

Please sign in to comment.