Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run middleware parser handler only for middleware modules #31219

Merged
merged 3 commits into from Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 10 additions & 6 deletions packages/next/build/webpack/plugins/middleware-plugin.ts
Expand Up @@ -205,7 +205,12 @@ export default class MiddlewarePlugin {
})

const handler = (parser: webpack5.javascript.JavascriptParser) => {
const isMiddlewareModule = () =>
parser.state.module && parser.state.module.layer === 'middleware'

const wrapExpression = (expr: any) => {
if (!isMiddlewareModule()) return

if (dev) {
const dep1 = new wp.dependencies.ConstDependency(
'__next_eval__(function() { return ',
Expand Down Expand Up @@ -242,10 +247,14 @@ export default class MiddlewarePlugin {
}

const expressionHandler = () => {
if (!isMiddlewareModule()) return

wp.optimize.InnerGraph.onUsage(parser.state, flagModule)
}

const ignore = () => {
if (!isMiddlewareModule()) return

return true
}

Expand Down Expand Up @@ -282,12 +291,7 @@ export default class MiddlewarePlugin {
.tap(PLUGIN_NAME, ignore)

const memberChainHandler = (_expr: any, members: string[]) => {
if (
!parser.state.module ||
parser.state.module.layer !== 'middleware'
) {
return
}
if (!isMiddlewareModule()) return

if (members.length >= 2 && members[0] === 'env') {
const envName = members[1]
Expand Down
5 changes: 5 additions & 0 deletions test/integration/middleware/with-eval/lib/utils.js
Expand Up @@ -6,3 +6,8 @@ export async function usingEval() {
export async function notUsingEval() {
return { value: 100 }
}

export function usingEvalSync() {
// eslint-disable-next-line no-eval
return { value: eval('100') }
}
15 changes: 15 additions & 0 deletions test/integration/middleware/with-eval/pages/index.js
@@ -0,0 +1,15 @@
import { usingEvalSync, usingEval } from '../lib/utils'

export async function getServerSideProps() {
return {
props: await usingEval(),
}
}

export default function Page(props) {
return (
<div>
{props.value} and {usingEvalSync().value}
</div>
)
}
11 changes: 10 additions & 1 deletion test/integration/middleware/with-eval/test/index.test.js
Expand Up @@ -8,6 +8,7 @@ import {
killApp,
launchApp,
nextBuild,
renderViaHTTP,
waitFor,
} from 'next-test-utils'

Expand Down Expand Up @@ -45,7 +46,8 @@ describe('Middleware usage of dynamic code evaluation', () => {
expect(output).toContain(DYNAMIC_CODE_ERROR)
expect(output).toContain('DynamicCodeEvaluationWarning')
expect(output).toContain('pages/_middleware')
expect(output).toContain('lib/utils.js')
// TODO check why that has a backslash on windows
expect(output).toMatch(/lib[\\/]utils\.js/)
expect(output).toContain('usingEval')
expect(stripAnsi(output)).toContain("value: eval('100')")
})
Expand All @@ -57,6 +59,13 @@ describe('Middleware usage of dynamic code evaluation', () => {
expect(json.value).toEqual(100)
expect(output).not.toContain(DYNAMIC_CODE_ERROR)
})

it('does not has problems with eval in page or server code', async () => {
const html = await renderViaHTTP(context.appPort, `/`)
expect(html).toMatch(/>100<!-- --> and <!-- -->100<\//)
await waitFor(500)
expect(output).not.toContain(DYNAMIC_CODE_ERROR)
})
})

describe('production mode', () => {
Expand Down