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 {
useHtmlContext,
} 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 }
......@@ -89,7 +90,9 @@ function getPolyfillScripts(context: HtmlProps, props: OriginProps) {
nonce={props.nonce}
crossOrigin={props.crossOrigin || crossOrigin}
noModule={true}
src={`${assetPrefix}/_next/${polyfill}${assetQueryString}`}
src={`${assetPrefix}/_next/${encodeURIPath(
polyfill
)}${assetQueryString}`}
/>
))
}
......@@ -164,7 +167,7 @@ function getDynamicChunks(
async={!isDevelopment && disableOptimizedLoading}
defer={!disableOptimizedLoading}
key={file}
src={`${assetPrefix}/_next/${encodeURI(file)}${assetQueryString}`}
src={`${assetPrefix}/_next/${encodeURIPath(file)}${assetQueryString}`}
nonce={props.nonce}
crossOrigin={props.crossOrigin || crossOrigin}
/>
......@@ -195,7 +198,7 @@ function getScripts(
return (
<script
key={file}
src={`${assetPrefix}/_next/${encodeURI(file)}${assetQueryString}`}
src={`${assetPrefix}/_next/${encodeURIPath(file)}${assetQueryString}`}
nonce={props.nonce}
async={!isDevelopment && disableOptimizedLoading}
defer={!disableOptimizedLoading}
......@@ -399,7 +402,7 @@ function getNextFontLinkTags(
<link
key={fontFile}
rel="preload"
href={`${assetPrefix}/_next/${encodeURI(fontFile)}`}
href={`${assetPrefix}/_next/${encodeURIPath(fontFile)}`}
as="font"
type={`font/${ext}`}
crossOrigin="anonymous"
......@@ -459,7 +462,9 @@ export class Head extends React.Component<HeadProps> {
key={`${file}-preload`}
nonce={this.props.nonce}
rel="preload"
href={`${assetPrefix}/_next/${encodeURI(file)}${assetQueryString}`}
href={`${assetPrefix}/_next/${encodeURIPath(
file
)}${assetQueryString}`}
as="style"
crossOrigin={this.props.crossOrigin || crossOrigin}
/>
......@@ -472,7 +477,9 @@ export class Head extends React.Component<HeadProps> {
key={file}
nonce={this.props.nonce}
rel="stylesheet"
href={`${assetPrefix}/_next/${encodeURI(file)}${assetQueryString}`}
href={`${assetPrefix}/_next/${encodeURIPath(
file
)}${assetQueryString}`}
crossOrigin={this.props.crossOrigin || crossOrigin}
data-n-g={isUnmanagedFile ? undefined : isSharedFile ? '' : undefined}
data-n-p={isUnmanagedFile ? undefined : isSharedFile ? undefined : ''}
......@@ -504,7 +511,7 @@ export class Head extends React.Component<HeadProps> {
<link
rel="preload"
key={file}
href={`${assetPrefix}/_next/${encodeURI(
href={`${assetPrefix}/_next/${encodeURIPath(
file
)}${assetQueryString}`}
as="script"
......@@ -541,7 +548,9 @@ export class Head extends React.Component<HeadProps> {
key={file}
nonce={this.props.nonce}
rel="preload"
href={`${assetPrefix}/_next/${encodeURI(file)}${assetQueryString}`}
href={`${assetPrefix}/_next/${encodeURIPath(
file
)}${assetQueryString}`}
as="script"
crossOrigin={this.props.crossOrigin || crossOrigin}
/>
......@@ -1094,7 +1103,9 @@ export class NextScript extends React.Component<OriginProps> {
{ampDevFiles.map((file) => (
<script
key={file}
src={`${assetPrefix}/_next/${file}${assetQueryString}`}
src={`${assetPrefix}/_next/${encodeURIPath(
file
)}${assetQueryString}`}
nonce={this.props.nonce}
crossOrigin={this.props.crossOrigin || crossOrigin}
data-ampdevmode
......@@ -1123,7 +1134,7 @@ export class NextScript extends React.Component<OriginProps> {
? buildManifest.devFiles.map((file: string) => (
<script
key={file}
src={`${assetPrefix}/_next/${encodeURI(
src={`${assetPrefix}/_next/${encodeURIPath(
file
)}${assetQueryString}`}
nonce={this.props.nonce}
......
......@@ -3,6 +3,7 @@ import { interopDefault } from './interop-default'
import { getLinkAndScriptTags } from './get-css-inlined-link-tags'
import type { AppRenderContext } from './app-render'
import { getAssetQueryString } from './get-asset-query-string'
import { encodeURIPath } from '../../shared/lib/encode-uri-path'
export async function createComponentStylesAndScripts({
filePath,
......@@ -26,10 +27,9 @@ export async function createComponentStylesAndScripts({
const styles = cssHrefs
? cssHrefs.map((href, index) => {
const fullHref = `${ctx.assetPrefix}/_next/${href}${getAssetQueryString(
ctx,
true
)}`
const fullHref = `${ctx.assetPrefix}/_next/${encodeURIPath(
href
)}${getAssetQueryString(ctx, true)}`
// `Precedence` is an opt-in signal for React to handle resource
// loading and deduplication, etc. It's also used as the key to sort
......@@ -55,7 +55,12 @@ export async function createComponentStylesAndScripts({
const scripts = jsHrefs
? jsHrefs.map((href) => (
<script src={`${ctx.assetPrefix}/_next/${href}`} async={true} />
<script
src={`${ctx.assetPrefix}/_next/${encodeURIPath(
href
)}${getAssetQueryString(ctx, true)}`}
async={true}
/>
))
: 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