Unverified Commit f6baf61b authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

fix encoding for filenames containing `?` or `#` (#58293)

Closes PACK-1935

---------
Co-authored-by: 's avatarWill Binns-Smith <wbinnssmith@gmail.com>
parent 027d7ab1
...@@ -24,6 +24,7 @@ import { ...@@ -24,6 +24,7 @@ import {
useHtmlContext, useHtmlContext,
} from '../shared/lib/html-context.shared-runtime' } from '../shared/lib/html-context.shared-runtime'
import type { HtmlProps } from '../shared/lib/html-context.shared-runtime' import type { HtmlProps } from '../shared/lib/html-context.shared-runtime'
import { encodeURIPath } from '../shared/lib/encode-uri-path'
export type { DocumentContext, DocumentInitialProps, DocumentProps } export type { DocumentContext, DocumentInitialProps, DocumentProps }
...@@ -89,7 +90,9 @@ function getPolyfillScripts(context: HtmlProps, props: OriginProps) { ...@@ -89,7 +90,9 @@ function getPolyfillScripts(context: HtmlProps, props: OriginProps) {
nonce={props.nonce} nonce={props.nonce}
crossOrigin={props.crossOrigin || crossOrigin} crossOrigin={props.crossOrigin || crossOrigin}
noModule={true} noModule={true}
src={`${assetPrefix}/_next/${polyfill}${assetQueryString}`} src={`${assetPrefix}/_next/${encodeURIPath(
polyfill
)}${assetQueryString}`}
/> />
)) ))
} }
...@@ -164,7 +167,7 @@ function getDynamicChunks( ...@@ -164,7 +167,7 @@ function getDynamicChunks(
async={!isDevelopment && disableOptimizedLoading} async={!isDevelopment && disableOptimizedLoading}
defer={!disableOptimizedLoading} defer={!disableOptimizedLoading}
key={file} key={file}
src={`${assetPrefix}/_next/${encodeURI(file)}${assetQueryString}`} src={`${assetPrefix}/_next/${encodeURIPath(file)}${assetQueryString}`}
nonce={props.nonce} nonce={props.nonce}
crossOrigin={props.crossOrigin || crossOrigin} crossOrigin={props.crossOrigin || crossOrigin}
/> />
...@@ -195,7 +198,7 @@ function getScripts( ...@@ -195,7 +198,7 @@ function getScripts(
return ( return (
<script <script
key={file} key={file}
src={`${assetPrefix}/_next/${encodeURI(file)}${assetQueryString}`} src={`${assetPrefix}/_next/${encodeURIPath(file)}${assetQueryString}`}
nonce={props.nonce} nonce={props.nonce}
async={!isDevelopment && disableOptimizedLoading} async={!isDevelopment && disableOptimizedLoading}
defer={!disableOptimizedLoading} defer={!disableOptimizedLoading}
...@@ -399,7 +402,7 @@ function getNextFontLinkTags( ...@@ -399,7 +402,7 @@ function getNextFontLinkTags(
<link <link
key={fontFile} key={fontFile}
rel="preload" rel="preload"
href={`${assetPrefix}/_next/${encodeURI(fontFile)}`} href={`${assetPrefix}/_next/${encodeURIPath(fontFile)}`}
as="font" as="font"
type={`font/${ext}`} type={`font/${ext}`}
crossOrigin="anonymous" crossOrigin="anonymous"
...@@ -459,7 +462,9 @@ export class Head extends React.Component<HeadProps> { ...@@ -459,7 +462,9 @@ export class Head extends React.Component<HeadProps> {
key={`${file}-preload`} key={`${file}-preload`}
nonce={this.props.nonce} nonce={this.props.nonce}
rel="preload" rel="preload"
href={`${assetPrefix}/_next/${encodeURI(file)}${assetQueryString}`} href={`${assetPrefix}/_next/${encodeURIPath(
file
)}${assetQueryString}`}
as="style" as="style"
crossOrigin={this.props.crossOrigin || crossOrigin} crossOrigin={this.props.crossOrigin || crossOrigin}
/> />
...@@ -472,7 +477,9 @@ export class Head extends React.Component<HeadProps> { ...@@ -472,7 +477,9 @@ export class Head extends React.Component<HeadProps> {
key={file} key={file}
nonce={this.props.nonce} nonce={this.props.nonce}
rel="stylesheet" rel="stylesheet"
href={`${assetPrefix}/_next/${encodeURI(file)}${assetQueryString}`} href={`${assetPrefix}/_next/${encodeURIPath(
file
)}${assetQueryString}`}
crossOrigin={this.props.crossOrigin || crossOrigin} crossOrigin={this.props.crossOrigin || crossOrigin}
data-n-g={isUnmanagedFile ? undefined : isSharedFile ? '' : undefined} data-n-g={isUnmanagedFile ? undefined : isSharedFile ? '' : undefined}
data-n-p={isUnmanagedFile ? undefined : isSharedFile ? undefined : ''} data-n-p={isUnmanagedFile ? undefined : isSharedFile ? undefined : ''}
...@@ -504,7 +511,7 @@ export class Head extends React.Component<HeadProps> { ...@@ -504,7 +511,7 @@ export class Head extends React.Component<HeadProps> {
<link <link
rel="preload" rel="preload"
key={file} key={file}
href={`${assetPrefix}/_next/${encodeURI( href={`${assetPrefix}/_next/${encodeURIPath(
file file
)}${assetQueryString}`} )}${assetQueryString}`}
as="script" as="script"
...@@ -541,7 +548,9 @@ export class Head extends React.Component<HeadProps> { ...@@ -541,7 +548,9 @@ export class Head extends React.Component<HeadProps> {
key={file} key={file}
nonce={this.props.nonce} nonce={this.props.nonce}
rel="preload" rel="preload"
href={`${assetPrefix}/_next/${encodeURI(file)}${assetQueryString}`} href={`${assetPrefix}/_next/${encodeURIPath(
file
)}${assetQueryString}`}
as="script" as="script"
crossOrigin={this.props.crossOrigin || crossOrigin} crossOrigin={this.props.crossOrigin || crossOrigin}
/> />
...@@ -1094,7 +1103,9 @@ export class NextScript extends React.Component<OriginProps> { ...@@ -1094,7 +1103,9 @@ export class NextScript extends React.Component<OriginProps> {
{ampDevFiles.map((file) => ( {ampDevFiles.map((file) => (
<script <script
key={file} key={file}
src={`${assetPrefix}/_next/${file}${assetQueryString}`} src={`${assetPrefix}/_next/${encodeURIPath(
file
)}${assetQueryString}`}
nonce={this.props.nonce} nonce={this.props.nonce}
crossOrigin={this.props.crossOrigin || crossOrigin} crossOrigin={this.props.crossOrigin || crossOrigin}
data-ampdevmode data-ampdevmode
...@@ -1123,7 +1134,7 @@ export class NextScript extends React.Component<OriginProps> { ...@@ -1123,7 +1134,7 @@ export class NextScript extends React.Component<OriginProps> {
? buildManifest.devFiles.map((file: string) => ( ? buildManifest.devFiles.map((file: string) => (
<script <script
key={file} key={file}
src={`${assetPrefix}/_next/${encodeURI( src={`${assetPrefix}/_next/${encodeURIPath(
file file
)}${assetQueryString}`} )}${assetQueryString}`}
nonce={this.props.nonce} nonce={this.props.nonce}
......
...@@ -3,6 +3,7 @@ import { interopDefault } from './interop-default' ...@@ -3,6 +3,7 @@ import { interopDefault } from './interop-default'
import { getLinkAndScriptTags } from './get-css-inlined-link-tags' import { getLinkAndScriptTags } from './get-css-inlined-link-tags'
import type { AppRenderContext } from './app-render' import type { AppRenderContext } from './app-render'
import { getAssetQueryString } from './get-asset-query-string' import { getAssetQueryString } from './get-asset-query-string'
import { encodeURIPath } from '../../shared/lib/encode-uri-path'
export async function createComponentStylesAndScripts({ export async function createComponentStylesAndScripts({
filePath, filePath,
...@@ -26,10 +27,9 @@ export async function createComponentStylesAndScripts({ ...@@ -26,10 +27,9 @@ export async function createComponentStylesAndScripts({
const styles = cssHrefs const styles = cssHrefs
? cssHrefs.map((href, index) => { ? cssHrefs.map((href, index) => {
const fullHref = `${ctx.assetPrefix}/_next/${href}${getAssetQueryString( const fullHref = `${ctx.assetPrefix}/_next/${encodeURIPath(
ctx, href
true )}${getAssetQueryString(ctx, true)}`
)}`
// `Precedence` is an opt-in signal for React to handle resource // `Precedence` is an opt-in signal for React to handle resource
// loading and deduplication, etc. It's also used as the key to sort // loading and deduplication, etc. It's also used as the key to sort
...@@ -55,7 +55,12 @@ export async function createComponentStylesAndScripts({ ...@@ -55,7 +55,12 @@ export async function createComponentStylesAndScripts({
const scripts = jsHrefs const scripts = jsHrefs
? jsHrefs.map((href) => ( ? jsHrefs.map((href) => (
<script src={`${ctx.assetPrefix}/_next/${href}`} async={true} /> <script
src={`${ctx.assetPrefix}/_next/${encodeURIPath(
href
)}${getAssetQueryString(ctx, true)}`}
async={true}
/>
)) ))
: null : null
......
export function encodeURIPath(file: string) {
return file
.split('/')
.map((p) => encodeURIComponent(p))
.join('/')
}
'use client'
export default function Client() {
return <p>hello world</p>
}
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import Compoment from './client#component'
export default function Page() {
return <Compoment />
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}
module.exports = nextConfig
import dynamic from 'next/dynamic'
const Component = dynamic(() => import('../app/client#component'))
export default function Page() {
return <Component />
}
/* eslint-disable jest/no-standalone-expect */
import { nextTestSetup } from 'e2e-utils'
describe('weird chars in scripts', () => {
const { next } = nextTestSetup({
files: __dirname,
})
// TODO: fix test case in webpack
// It's failing with `Could not find the module ".../app/client#component.tsx#" in the React Client Manifest. This is probably a bug in the React Server Components bundler.`
;(process.env.TURBOPACK ? it : it.skip)(
'should load in the browser',
async () => {
const browser = await next.browser('/')
expect(await browser.elementByCss('p').text()).toBe('hello world')
const scripts = await browser.elementsByCss('script')
for (const script of scripts) {
const src = await script.evaluate((script) => script.src)
expect(src).not.toContain('#')
}
}
)
;(process.env.TURBOPACK ? it : it.skip)(
'should load in the browser',
async () => {
const browser = await next.browser('/pages')
expect(await browser.elementByCss('p').text()).toBe('hello world')
const scripts = await browser.elementsByCss('script')
for (const script of scripts) {
const src = await script.evaluate((script) => script.src)
expect(src).not.toContain('#')
}
}
)
})
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