Unverified Commit e0399ca6 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Support next/og usage in ESM nextjs app (#60818)

### What 

Follow up for #59852 , now you can use `next/og` if your nextjs app is
marked as ESM with `"type": "module"` in package.json.

### How

It's a bug in external handling, we shouldn't ESM import error for local
requests. Previously you'll see the below error but the
og import shouldn't be errored as it's not external package

```
Module not found: ESM packages (/.../app/opengraph-image.js) need to be imported. Use 'import' to reference the package instead
 https://nextjs.org/docs/messages/import-esm-externals
```


Closes NEXT-2147
parent 299d1453
...@@ -328,7 +328,7 @@ export function makeExternalHandler({ ...@@ -328,7 +328,7 @@ export function makeExternalHandler({
// ESM externals can only be imported (and not required). // ESM externals can only be imported (and not required).
// Make an exception in loose mode. // Make an exception in loose mode.
if (!isEsmRequested && isEsm && !looseEsmExternals) { if (!isEsmRequested && isEsm && !looseEsmExternals && !isLocal) {
throw new Error( 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` `ESM packages (${request}) need to be imported. Use 'import' to reference the package instead. https://nextjs.org/docs/messages/import-esm-externals`
) )
......
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>
)
)
}
...@@ -35,5 +35,11 @@ createNextDescribe( ...@@ -35,5 +35,11 @@ createNextDescribe(
) )
expect(await $('.root').text()).toContain('pages') 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')
})
} }
) )
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment