Skip to content

Commit

Permalink
Demo Site: don't exclude middleware for /api/... requests (#3086)
Browse files Browse the repository at this point in the history
  • Loading branch information
nsams authored Jan 14, 2025
1 parent 9f69e7b commit dab3213
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 7 deletions.
2 changes: 1 addition & 1 deletion demo/admin/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function App() {
siteConfig.scope.domain === "secondary"
? `${siteConfig.url}/block-preview`
: `${siteConfig.url}/block-preview/${scope.domain}/${scope.language}`,
sitePreviewApiUrl: `${siteConfig.url}/api/site-preview`,
sitePreviewApiUrl: `${siteConfig.url}/site-preview`,
};
},
}}
Expand Down
File renamed without changes.
File renamed without changes.
7 changes: 3 additions & 4 deletions demo/site/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { withAdminRedirectMiddleware } from "./middleware/adminRedirect";
import { withBlockPreviewMiddleware } from "./middleware/blockPreview";
import { chain } from "./middleware/chain";
import { withCspHeadersMiddleware } from "./middleware/cspHeaders";
import { withDamRewriteMiddleware } from "./middleware/damRewrite";
import { withDomainRewriteMiddleware } from "./middleware/domainRewrite";
import { withPredefinedPagesMiddleware } from "./middleware/predefinedPages";
import { withPreviewMiddleware } from "./middleware/preview";
import { withRedirectToMainHostMiddleware } from "./middleware/redirectToMainHost";
import { withSitePreviewMiddleware } from "./middleware/sitePreview";

Expand All @@ -14,7 +14,7 @@ export default chain([
withAdminRedirectMiddleware,
withDamRewriteMiddleware,
withCspHeadersMiddleware, // order matters: after redirects (that don't need csp headers), before everything else that needs csp headers
withBlockPreviewMiddleware,
withPreviewMiddleware,
withPredefinedPagesMiddleware,
withDomainRewriteMiddleware, // must be last (rewrites all urls)
]);
Expand All @@ -23,14 +23,13 @@ export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico, favicon.svg, favicon.png
* - manifest.json
* - robots.txt
*/
"/((?!api|_next/static|_next/image|favicon.ico|favicon.svg|favicon.png|manifest.json|robots.txt).*)",
"/((?!_next/static|_next/image|favicon.ico|favicon.svg|favicon.png|manifest.json|robots.txt).*)",
],
// TODO find a better solution for this (https://nextjs.org/docs/messages/edge-dynamic-code-evaluation)
unstable_allowDynamic: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { NextRequest, NextResponse } from "next/server";

import { CustomMiddleware } from "./chain";

export function withBlockPreviewMiddleware(middleware: CustomMiddleware) {
export function withPreviewMiddleware(middleware: CustomMiddleware) {
return async (request: NextRequest) => {
if (request.nextUrl.pathname.startsWith("/block-preview/")) {
if (request.nextUrl.pathname.startsWith("/block-preview/") || request.nextUrl.pathname.startsWith("/site-preview/")) {
// don't apply any other middlewares
return NextResponse.next();
}
Expand Down

0 comments on commit dab3213

Please sign in to comment.