From 93a24df78b0e1c9bf6879f462b1f8b78fc11b739 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Fri, 7 Apr 2023 17:21:14 +0200 Subject: [PATCH] Support og image with edge if there's match, no more ensurance rm old --- packages/next/src/server/next-server.ts | 8 ++++-- .../app/(group)/twitter-image.tsx | 27 +++++++++++++++++++ .../metadata-dynamic-routes/index.test.ts | 27 ++++++++++++++++--- 3 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 test/e2e/app-dir/metadata-dynamic-routes/app/(group)/twitter-image.tsx diff --git a/packages/next/src/server/next-server.ts b/packages/next/src/server/next-server.ts index 780807f237b3..08b7d40c5178 100644 --- a/packages/next/src/server/next-server.ts +++ b/packages/next/src/server/next-server.ts @@ -24,6 +24,7 @@ import { Params, } from '../shared/lib/router/utils/route-matcher' import type { MiddlewareRouteMatch } from '../shared/lib/router/utils/middleware-route-matcher' +import type { RouteMatch } from './future/route-matches/route-match' import fs from 'fs' import { join, relative, resolve, sep } from 'path' @@ -1520,6 +1521,7 @@ export default class NextNodeServer extends BaseServer { query, params: match.params, page: match.definition.page, + match, appPaths: null, }) @@ -2563,13 +2565,15 @@ export default class NextNodeServer extends BaseServer { params: Params | undefined page: string appPaths: string[] | null + match?: RouteMatch onWarning?: (warning: Error) => void }): Promise { let edgeInfo: ReturnType | undefined - const { query, page } = params + const { query, page, match } = params - await this.ensureEdgeFunction({ page, appPaths: params.appPaths }) + if (!match) + await this.ensureEdgeFunction({ page, appPaths: params.appPaths }) edgeInfo = this.getEdgeFunctionInfo({ page, middleware: false, diff --git a/test/e2e/app-dir/metadata-dynamic-routes/app/(group)/twitter-image.tsx b/test/e2e/app-dir/metadata-dynamic-routes/app/(group)/twitter-image.tsx new file mode 100644 index 000000000000..9b3e9c81b0b9 --- /dev/null +++ b/test/e2e/app-dir/metadata-dynamic-routes/app/(group)/twitter-image.tsx @@ -0,0 +1,27 @@ +import { ImageResponse } from 'next/server' + +export const alt = 'Twitter' +export const size = { width: 1200, height: 675 } + +export default function twitter() { + return new ImageResponse( + ( +
+ group route twitter +
+ ), + size + ) +} + +export const runtime = 'edge' diff --git a/test/e2e/app-dir/metadata-dynamic-routes/index.test.ts b/test/e2e/app-dir/metadata-dynamic-routes/index.test.ts index 07ddba2c6c6c..410296d20e48 100644 --- a/test/e2e/app-dir/metadata-dynamic-routes/index.test.ts +++ b/test/e2e/app-dir/metadata-dynamic-routes/index.test.ts @@ -15,7 +15,7 @@ createNextDescribe( files: __dirname, skipDeployment: true, }, - ({ next, isNextDev, isNextDeploy }) => { + ({ next, isNextDev, isNextStart, isNextDeploy }) => { describe('text routes', () => { it('should handle robots.[ext] dynamic routes', async () => { const res = await next.fetch('/robots.txt') @@ -162,13 +162,21 @@ createNextDescribe( it('should generate unique path for image routes under group routes', async () => { const $ = await next.render$('/blog') const ogImageUrl = $('meta[property="og:image"]').attr('content') + const twitterImageUrl = $('meta[name="twitter:image"]').attr('content') const ogImageUrlInstance = new URL(ogImageUrl) - const res = await next.fetch(ogImageUrlInstance.pathname) + const twitterImageUrlInstance = new URL(twitterImageUrl) + + const resOg = await next.fetch(ogImageUrlInstance.pathname) + const resTwitter = await next.fetch(twitterImageUrlInstance.pathname) // generate unique path with suffix for image routes under group routes expect(ogImageUrl).toMatch(/opengraph-image-\w{6}\?/) expect(ogImageUrl).toMatch(hashRegex) - expect(res.status).toBe(200) + expect(twitterImageUrl).toMatch(/twitter-image-\w{6}\?/) + expect(twitterImageUrl).toMatch(hashRegex) + + expect(resOg.status).toBe(200) + expect(resTwitter.status).toBe(200) }) it('should inject dynamic metadata properly to head', async () => { @@ -224,5 +232,18 @@ createNextDescribe( 'Twitter' ) }) + + if (isNextStart) { + it('should support edge runtime of image routes', async () => { + const middlewareManifest = JSON.parse( + await next.readFile('.next/server/middleware-manifest.json') + ) + const functionRoutes = Object.keys(middlewareManifest.functions) + const edgeRoute = functionRoutes.find((route) => + route.startsWith('/(group)/twitter-image-') + ) + expect(edgeRoute).toMatch(/\/\(group\)\/twitter-image-\w{6}\/route/) + }) + } } )