Skip to content

Commit

Permalink
Merge branch 'develop' into dependabot/github_actions/dot-github/work…
Browse files Browse the repository at this point in the history
…flows/github_actions-5ebe5a9988
  • Loading branch information
nicholasio authored Dec 23, 2024
2 parents 9361504 + 695a0fd commit 160cc56
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/orange-shirts-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@headstartwp/next": patch
---

Ensure query string is appended to URL rewrite/redirect in middleware
1 change: 1 addition & 0 deletions .changeset/pre.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"large-spies-give",
"late-rings-design",
"moody-emus-matter",
"orange-shirts-relax",
"perfect-cobras-knock",
"perfect-pumas-approve",
"poor-donkeys-march",
Expand Down
6 changes: 6 additions & 0 deletions packages/next/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @headstartwp/next

## 1.5.0-next.8

### Patch Changes

- 9f7da69: Ensure query string is appended to URL rewrite/redirect in middleware

## 1.5.0-next.7

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@headstartwp/next",
"version": "1.5.0-next.7",
"version": "1.5.0-next.8",
"description": "`@headstartwp/next` is the Next.js bindings for the headless framework.",
"homepage": "https://github.com/10up/headstartwp/blob/develop/packages/next/README.md",
"license": "MIT",
Expand Down
55 changes: 55 additions & 0 deletions packages/next/src/middlewares/__tests__/appMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,51 @@ describe('appMiddleware', () => {
expect(res.headers.get('x-headstartwp-locale')).toBe('pt');
});

it('[multisite] supports query strings in App and Pages router', async () => {
setHeadstartWPConfig({
sites: [
{
sourceUrl: 'http://testwp.com',
hostUrl: 'http://test.com',
},
{
sourceUrl: 'http://testwp2.com',
hostUrl: 'http://test2.com',
},
{
sourceUrl: 'http://testwp2.com/en',
hostUrl: 'http://test2.com',
},
],
});

// App Router
let req = new NextRequest('http://test2.com/post-name?s=search-slug', {
method: 'GET',
});

req.headers.set('host', 'test2.com');

let res = await AppMiddleware(req, { appRouter: true });

expect(res.headers.get('x-middleware-rewrite')).toBe(
'http://test2.com/test2.com/post-name?s=search-slug',
);

// Pages Router
req = new NextRequest('http://test2.com/post-name?s=search-slug', {
method: 'GET',
});

req.headers.set('host', 'test2.com');

res = await AppMiddleware(req, { appRouter: false });

expect(res.headers.get('x-middleware-rewrite')).toBe(
'http://test2.com/_sites/test2.com/post-name?s=search-slug',
);
});

it('[polylang] supports locales with app router', async () => {
setHeadstartWPConfig({
sourceUrl: 'http://testwp.com',
Expand Down Expand Up @@ -343,6 +388,16 @@ describe('appMiddleware', () => {

res = await AppMiddleware(req, { appRouter: true });
expect(res.headers.get('x-middleware-rewrite')).toBe('http://test.com/en/post-name');

// add default locale with a query string
req = new NextRequest('http://test.com/post-name?s=query', {
method: 'GET',
});

res = await AppMiddleware(req, { appRouter: true });
expect(res.headers.get('x-middleware-rewrite')).toBe(
'http://test.com/en/post-name?s=query',
);
});

it('[polylang no locale detection] supports locales with app router', async () => {
Expand Down
20 changes: 13 additions & 7 deletions packages/next/src/middlewares/appMidleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export async function AppMiddleware(
options: AppMidlewareOptions = { appRouter: false },
) {
let response = NextResponse.next();
const { pathname } = req.nextUrl;
const { pathname, searchParams } = req.nextUrl;

if (isStaticAssetRequest(req) || isInternalRequest(req)) {
return response;
Expand All @@ -120,6 +120,9 @@ export async function AppMiddleware(
const site = getSiteByHost(hostname, !hasPolylangIntegration ? locale : undefined);
const isMultisiteRequest = site !== null && typeof site.sourceUrl !== 'undefined';

// ensure we re-add the query string when rewriting/redirecing
const queryString = Array.from(searchParams.keys()).length ? `?${searchParams.toString()}` : '';

const {
redirectStrategy,
sourceUrl,
Expand Down Expand Up @@ -161,7 +164,7 @@ export async function AppMiddleware(
shouldRedirect = true;
const pathNameWithoutLocale = pathname.replace(`/${locale}`, '');
response = NextResponse.redirect(
new URL(pathNameWithoutLocale, req.url.replace(`/${locale}`, '')),
new URL(pathNameWithoutLocale + queryString, req.url.replace(`/${locale}`, '')),
);
}
// if we detected a non-default locale, there isn't a supported locale in the URL already
Expand All @@ -175,14 +178,17 @@ export async function AppMiddleware(
) {
shouldRedirect = true;
response = NextResponse.redirect(
new URL(`/${locale}${pathname.startsWith('/') ? '' : '/'}${pathname}`, req.url),
new URL(
`/${locale}${pathname.startsWith('/') ? '' : '/'}${pathname}${queryString}`,
req.url,
),
);
}
// nothing else and there's not a locale in path then rewrite to add default locale
else if (pathnameIsMissingLocale && !isValidLocale(firstPathSlice)) {
response = NextResponse.rewrite(
new URL(
`/${defaultAppRouterLocale}${pathname.startsWith('/') ? '' : '/'}${pathname}`,
`/${defaultAppRouterLocale}${pathname.startsWith('/') ? '' : '/'}${pathname}${queryString}`,
req.url,
),
);
Expand All @@ -191,10 +197,10 @@ export async function AppMiddleware(

if (isMultisiteRequest && !shouldRedirect) {
const hostNameOrSlug = site.slug || hostname;
const pagesRouterRewrite = `/_sites/${hostNameOrSlug}${pathname}`;
const pagesRouterRewrite = `/_sites/${hostNameOrSlug}${pathname}${queryString}`;
const appRouterRewrite = locale
? `/${locale}/${hostNameOrSlug}${pathname.replace(`/${locale}`, '')}`
: `/${hostNameOrSlug}${pathname}`;
? `/${locale}/${hostNameOrSlug}${pathname.replace(`/${locale}`, '')}${queryString}`
: `/${hostNameOrSlug}${pathname}${queryString}`;

response = NextResponse.rewrite(
new URL(options.appRouter ? appRouterRewrite : pagesRouterRewrite, req.nextUrl),
Expand Down
2 changes: 1 addition & 1 deletion projects/vite-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"react": "^18.3.1",
"react-dom": "^18.3.1",
"@headstartwp/core": "^1.5.0-next.7",
"@headstartwp/next": "^1.5.0-next.7"
"@headstartwp/next": "^1.5.0-next.8"
},
"devDependencies": {
"@10up/eslint-config": "^4.0.0",
Expand Down
2 changes: 1 addition & 1 deletion projects/wp-multisite-i18n-nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"dependencies": {
"@headstartwp/core": "^1.5.0-next.7",
"@headstartwp/next": "^1.5.0-next.7",
"@headstartwp/next": "^1.5.0-next.8",
"@linaria/core": "^6.2.0",
"@linaria/react": "^6.2.1",
"clsx": "^1.1.1",
Expand Down
2 changes: 1 addition & 1 deletion projects/wp-multisite-nextjs-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"react-dom": "^18",
"next": "^14.2.5",
"@headstartwp/core": "^1.5.0-next.7",
"@headstartwp/next": "^1.5.0-next.7"
"@headstartwp/next": "^1.5.0-next.8"
},
"devDependencies": {
"@10up/eslint-config": "^4.0.0",
Expand Down
2 changes: 1 addition & 1 deletion projects/wp-multisite-nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"dependencies": {
"@headstartwp/core": "^1.5.0-next.7",
"@headstartwp/next": "^1.5.0-next.7",
"@headstartwp/next": "^1.5.0-next.8",
"@linaria/core": "^6.2.0",
"@linaria/react": "^6.2.1",
"clsx": "^1.1.1",
Expand Down
2 changes: 1 addition & 1 deletion projects/wp-nextjs-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"react-dom": "^18",
"next": "^14.2.5",
"@headstartwp/core": "^1.5.0-next.7",
"@headstartwp/next": "^1.5.0-next.7"
"@headstartwp/next": "^1.5.0-next.8"
},
"devDependencies": {
"@10up/eslint-config": "^4.0.0",
Expand Down
2 changes: 1 addition & 1 deletion projects/wp-nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"dependencies": {
"@headstartwp/core": "^1.5.0-next.7",
"@headstartwp/next": "^1.5.0-next.7",
"@headstartwp/next": "^1.5.0-next.8",
"@10up/next-redis-cache-provider": "^1.0.0",
"@linaria/core": "^6.2.0",
"@linaria/react": "^6.2.1",
Expand Down
2 changes: 1 addition & 1 deletion projects/wp-polylang-nextjs-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"react-dom": "^18",
"next": "^14.2.5",
"@headstartwp/core": "^1.5.0-next.7",
"@headstartwp/next": "^1.5.0-next.7"
"@headstartwp/next": "^1.5.0-next.8"
},
"devDependencies": {
"@10up/eslint-config": "^4.0.0",
Expand Down

0 comments on commit 160cc56

Please sign in to comment.