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

fix: data url handling in css-loader #34034

Merged
merged 3 commits into from
Feb 6, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions packages/next/build/webpack/loaders/css-loader/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ function normalizePath(file) {
return path.sep === '\\' ? file.replace(/\\/g, '/') : file
}

function fixedEncodeURIComponent(str) {
return str.replace(/[!'()*]/g, (c) => `%${c.charCodeAt(0).toString(16)}`)
}

function normalizeUrl(url, isStringValue) {
let normalizedUrl = url

Expand All @@ -51,10 +55,28 @@ function normalizeUrl(url, isStringValue) {
}

if (matchNativeWin32Path.test(url)) {
return decodeURIComponent(normalizedUrl)
try {
normalizedUrl = decodeURIComponent(normalizedUrl)
} catch (error) {
// Ignores invalid and broken URLs and try to resolve them as is
}

return normalizedUrl
}

normalizedUrl = unescape(normalizedUrl)

if (isDataUrl(url)) {
return fixedEncodeURIComponent(normalizedUrl)
}

try {
normalizedUrl = decodeURI(normalizedUrl)
} catch (error) {
// Ignores invalid and broken URLs and try to resolve them as is
}

return decodeURIComponent(unescape(normalizedUrl))
return normalizedUrl
}

function requestify(url, rootContext) {
Expand Down
9 changes: 9 additions & 0 deletions test/integration/css-fixtures/data-url/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { className } from './index.module.css'

export default function Home() {
return (
<div id="verify-background" className={className}>
Background
</div>
)
}
4 changes: 4 additions & 0 deletions test/integration/css-fixtures/data-url/pages/index.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.className {
background: url('data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20649%2087.5%22%20xmlns%3Av%3D%22https%3A%2F%2Fvecta.io%2Fnano%22%3E%3Cpath%20d%3D%22M0%200h649v87.5H0z%22%20fill%3D%22%230b3647%22%2F%3E%3Cpath%20d%3D%22M0%204.03h649v76.22H0z%22%20fill%3D%22%23104f68%22%2F%3E%3Cpath%20d%3D%22M0%200h649v8.07H0z%22%20fill%3D%22%231a6184%22%2F%3E%3C%2Fsvg%3E')
0 0 no-repeat;
}
22 changes: 22 additions & 0 deletions test/integration/css/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1852,4 +1852,26 @@ describe('CSS Support', () => {
}
})
})

describe('Data URLs', () => {
const workDir = join(fixturesDir, 'data-url')

it('should compile successfully', async () => {
await remove(join(workDir, '.next'))
const { code } = await nextBuild(workDir)
expect(code).toBe(0)
})

it('should have emitted expected files', async () => {
const cssFolder = join(workDir, '.next/static/css')
const files = await readdir(cssFolder)
const cssFiles = files.filter((f) => /\.css$/.test(f))

expect(cssFiles.length).toBe(1)
const cssContent = await readFile(join(cssFolder, cssFiles[0]), 'utf8')
expect(cssContent.replace(/\/\*.*?\*\//g, '').trim()).toMatch(
/background:url\("data:[^"]+"\)/
)
})
})
})