-
Notifications
You must be signed in to change notification settings - Fork 27.1k
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { webpack } from 'next/dist/compiled/webpack/webpack' | ||
|
||
export default function nextErrorBrowserBinaryLoader( | ||
this: webpack.LoaderContext<any> | ||
) { | ||
const { resourcePath, rootContext } = this | ||
const relativePath = resourcePath.slice(rootContext.length + 1) | ||
throw new Error( | ||
`Node.js binary module ./${relativePath} is not supported in the browser. Please only use the module on server side` | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import type { webpack } from 'next/dist/compiled/webpack/webpack' | ||
import path from 'path' | ||
|
||
export default function nextErrorBrowserBinaryLoader( | ||
this: webpack.LoaderContext<any> | ||
) { | ||
let relativePath = path.relative(this.rootContext, this.resourcePath) | ||
if (!relativePath.startsWith('.')) { | ||
relativePath = './' + relativePath | ||
} | ||
return `module.exports = __non_webpack_require__(${JSON.stringify( | ||
relativePath | ||
)})` | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
import { RSC_CONTENT_TYPE_HEADER } from '../../client/components/app-router-headers' | ||
import RenderResult from '../render-result' | ||
import RenderResult, { type RenderResultOptions } from '../render-result' | ||
|
||
/** | ||
* Flight Response is always set to RSC_CONTENT_TYPE_HEADER to ensure it does not get interpreted as HTML. | ||
*/ | ||
export class FlightRenderResult extends RenderResult { | ||
constructor(response: string | ReadableStream<Uint8Array>) { | ||
super(response, { contentType: RSC_CONTENT_TYPE_HEADER, metadata: {} }) | ||
constructor( | ||
response: string | ReadableStream<Uint8Array>, | ||
options?: RenderResultOptions | ||
) { | ||
super(response, { | ||
contentType: RSC_CONTENT_TYPE_HEADER, | ||
waitUntil: options?.waitUntil, | ||
metadata: options?.metadata ?? {}, | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { ReactNode } from 'react' | ||
export default function Root({ children }: { children: ReactNode }) { | ||
return ( | ||
<html> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use client' | ||
|
||
import { foo } from 'foo-browser-import-binary' | ||
|
||
export default function Page() { | ||
return <p>{foo()}</p> | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { nextTestSetup } from 'e2e-utils' | ||
import { | ||
hasRedbox, | ||
getRedboxDescription, | ||
getRedboxSource, | ||
} from 'next-test-utils' | ||
;(process.env.TURBOPACK ? describe.skip : describe)( | ||
'externalize-node-binary-browser-error', | ||
() => { | ||
const { next } = nextTestSetup({ | ||
files: __dirname, | ||
}) | ||
|
||
it('should error when import node binary on browser side', async () => { | ||
const browser = await next.browser('/') | ||
await hasRedbox(browser) | ||
const redbox = { | ||
description: await getRedboxDescription(browser), | ||
source: await getRedboxSource(browser), | ||
} | ||
|
||
expect(redbox.description).toBe('Failed to compile') | ||
expect(redbox.source).toMatchInlineSnapshot(` | ||
"./node_modules/foo-browser-import-binary/binary.node | ||
Error: Node.js binary module ./node_modules/foo-browser-import-binary/binary.node is not supported in the browser. Please only use the module on server side" | ||
`) | ||
}) | ||
} | ||
) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { ReactNode } from 'react' | ||
export default function Root({ children }: { children: ReactNode }) { | ||
return ( | ||
<html> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { foo } from 'foo' | ||
|
||
export default function Page() { | ||
return <p>{foo()}</p> | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { nextTestSetup } from 'e2e-utils' | ||
|
||
describe('externalize-node-binary', () => { | ||
const { next } = nextTestSetup({ | ||
files: __dirname, | ||
}) | ||
|
||
it('should render correctly when node_modules require node binary module', async () => { | ||
const { status } = await next.fetch('/') | ||
expect(status).toBe(200) | ||
|
||
const browser = await next.browser('/') | ||
expect(await browser.elementByCss('p').text()).toBe('I am foo') | ||
}) | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use client' | ||
|
||
import { revalidate } from './actions/revalidate' | ||
|
||
export default function RevalidateViaForm({ tag }: { tag: string }) { | ||
const handleRevalidate = async () => { | ||
await revalidate(tag) | ||
} | ||
|
||
return ( | ||
<form action={handleRevalidate}> | ||
<button type="submit" id="submit-form" className="underline"> | ||
Revalidate via form | ||
</button> | ||
</form> | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use server' | ||
|
||
import { revalidateTag } from 'next/cache' | ||
|
||
export const revalidate = async ( | ||
tag: string | ||
): Promise<{ revalidated: boolean }> => { | ||
revalidateTag(tag) | ||
|
||
return { revalidated: true } | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { ReactNode } from 'react' | ||
|
||
export default function Root({ children }: { children: ReactNode }) { | ||
return ( | ||
<html> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |