-
Notifications
You must be signed in to change notification settings - Fork 26.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Alias nextjs api entry to esm version for app router (#59852)
## What When users specify `"type": "module"` in Next.js app, especially with `create-next-app`, `Image` component is not working. An error `Unsupported Server Component type: {...}` is thrown. ## Why `next/image` API is mixing with a client component as default export and a named export as server component. But the entry file of the API is still CJS file, which will import the module as the object. So you'll get `{ default, unstable_getImageProps }` when you do `import Image from 'next/image'` instead of `Image` component itself, where the CJS module load all the exports as an object. This is expected behavior for ESM but breaks the usage. It only errors when you're using js extensions, if you're using typescript, it still works. If you're using turbopack, it works in dev mode. This is also because webpack can't analyze the exports from CJS module of that `next/image` entry file. Usually we can assign the default export to the module itself, then attach other named exports onto it, so the default export equals the `module.exports` itself. But for `next/image` since the default export is an client component, doing that will error with React as you cannot modify the react client reference. Turbopack doesn't use the same way to analyze the default export, so it doesn't have this problem. ## How We create few ESM version of entry files of nextjs APIs, then pick up them to let app router for bundling, instead of using the `next/<api name>.js` CJS files. Those ESM entries still point to the `next/dist/..` CJS files. In this way webpack and directly gets the exports from the `next/dist/...` files and be aware of the module exports. No more CJS module wrapping the ESM module, the default and named exports can preserve correctly. Fixes #54777 Closes NEXT-1774 Closes NEXT-1879 Closes NEXT-1923
- Loading branch information
Showing
33 changed files
with
269 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from '../shared/lib/app-dynamic' | ||
export { default } from '../shared/lib/app-dynamic' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from '../pages/_app' | ||
export { default } from '../pages/_app' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from '../shared/lib/constants' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from '../pages/_document' | ||
export { default } from '../pages/_document' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default } from '../shared/lib/dynamic' | ||
export * from '../shared/lib/dynamic' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default } from '../shared/lib/head' | ||
export * from '../shared/lib/head' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from '../client/components/headers' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default } from '../shared/lib/image-external' | ||
export * from '../shared/lib/image-external' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default } from '../client/link' | ||
export * from '../client/link' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from '../client/components/navigation' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from '../server/og/image-response' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default } from '../client/router' | ||
export * from '../client/router' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default } from '../client/script' | ||
export * from '../client/script' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from '../server/web/exports/index' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use client' | ||
|
||
import { useRouter } from 'next/navigation.js' | ||
|
||
export function ClientHooks() { | ||
useRouter() | ||
return null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use client' | ||
|
||
import { useRouter } from 'next/navigation' | ||
|
||
export function ClientHooks() { | ||
useRouter() | ||
return null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import NextImage, { unstable_getImgProps } from 'next/image.js' | ||
import Link from 'next/link.js' | ||
import Script from 'next/script.js' | ||
|
||
import src from '../../public/test.jpg' | ||
|
||
export function Components() { | ||
return ( | ||
<> | ||
<NextImage className="img" src={src} /> | ||
<p className="unstable_getImgProps">{typeof unstable_getImgProps}</p> | ||
<Link className="link" href="/client"> | ||
link | ||
</Link> | ||
<Script className="script" src="/test-ext.js" /> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import NextImage, { unstable_getImgProps } from 'next/image' | ||
import Link from 'next/link' | ||
import Script from 'next/script' | ||
|
||
import src from '../../public/test.jpg' | ||
|
||
export function Components() { | ||
return ( | ||
<> | ||
<NextImage className="img" src={src} /> | ||
<p className="unstable_getImgProps">{typeof unstable_getImgProps}</p> | ||
<Link className="link" href="/client"> | ||
link | ||
</Link> | ||
<Script className="script" src="/test.js" /> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { ClientHooks } from './client-hooks-ext' | ||
import { headers, cookies } from 'next/headers.js' | ||
|
||
export function useHooks() { | ||
headers() | ||
cookies() | ||
return <ClientHooks /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { ClientHooks } from './client-hooks' | ||
import { headers, cookies } from 'next/headers' | ||
|
||
export function useHooks() { | ||
headers() | ||
cookies() | ||
return <ClientHooks /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { useHooks } from './hooks' | ||
import { useHooks as useHooks2 } from './hooks-ext' | ||
import { Components } from './components' | ||
import { Components as Components2 } from './components-ext' | ||
|
||
export default function Page() { | ||
useHooks() | ||
useHooks2() | ||
|
||
return ( | ||
<> | ||
<div id="without-ext"> | ||
<Components /> | ||
</div> | ||
<div id="with-ext"> | ||
<Components2 /> | ||
</div> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const metadata = { | ||
title: 'Next.js', | ||
description: 'Generated by Next.js', | ||
} | ||
|
||
export default function RootLayout({ children }) { | ||
return ( | ||
<html lang="en"> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
Oops, something went wrong.