Skip to content

Commit

Permalink
Support next/og usage in ESM nextjs app
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Jan 18, 2024
1 parent 486567d commit e7334d2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/next/src/build/handle-externals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ export function makeExternalHandler({

// ESM externals can only be imported (and not required).
// Make an exception in loose mode.
if (!isEsmRequested && isEsm && !looseEsmExternals) {
if (!isEsmRequested && isEsm && !looseEsmExternals && !isLocal) {
throw new Error(
`ESM packages (${request}) need to be imported. Use 'import' to reference the package instead. https://nextjs.org/docs/messages/import-esm-externals`
)
Expand Down
23 changes: 23 additions & 0 deletions test/e2e/app-dir/app-esm-js/app/opengraph-image.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ImageResponse } from 'next/og'

export default function OpenGraphImage() {
return new ImageResponse(
(
<div
style={{
width: 1200,
height: 630,
backgroundColor: '#000',
color: '#fff',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
flexDirection: 'column',
fontFamily: 'sans-serif',
}}
>
OG
</div>
)
)
}
6 changes: 6 additions & 0 deletions test/e2e/app-dir/app-esm-js/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,11 @@ createNextDescribe(
)
expect(await $('.root').text()).toContain('pages')
})

it('should support next/og image', async () => {
const res = await next.fetch('/opengraph-image')
expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toBe('image/png')
})
}
)

0 comments on commit e7334d2

Please sign in to comment.