-
Bug reportDescribe the bugIn this PR #16184, the To Reproduceimport React from 'react';
import { readFileSync } from 'fs';
import { resolve } from 'path';
import { Head } from 'next/document';
import type { DocumentContext } from 'next/document';
class CriticalHead extends Head {
getCssLinks() {
const cssFiles = this.context.files.filter(f => /\.css$/.test(f));
const { nonce } = this.props;
const cssLinks = cssFiles.map(file => {
const styleContent = readFileSync(resolve(process.cwd(), '.next', file), 'utf-8');
return <style key={file} nonce={nonce} dangerouslySetInnerHTML={{ __html: styleContent }} />;
});
return cssLinks.length ? cssLinks : null;
}
} System information
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
Hey @Timer can you confirm that this change is intended? If so I can look for a solution by myself |
Beta Was this translation helpful? Give feedback.
-
@bobaaaaa I am facing this issue as well now 🙋♂️ Offtopic: what does the |
Beta Was this translation helpful? Give feedback.
-
Because nobody of the nextjs core team answered my question: Here is our workaround. It's the same impl. @Timer used in the related PR. import { getPageFiles } from 'next/dist/next-server/server/get-page-files';
import type { BuildManifest } from 'next/dist/next-server/server/get-page-files';
type DocumentFiles = {
sharedFiles: readonly string[];
pageFiles: readonly string[];
allFiles: readonly string[];
};
function getDocumentFiles(buildManifest: BuildManifest, pathname: string): DocumentFiles {
const sharedFiles: readonly string[] = getPageFiles(buildManifest, '/_app');
const pageFiles: readonly string[] = pathname !== '/_error' ? getPageFiles(buildManifest, pathname) : [];
return {
sharedFiles,
pageFiles,
allFiles: [...new Set([...sharedFiles, ...pageFiles])],
};
}
class InlineStylesHead extends Head {
getCssLinks() {
return this.__getInlineStyles();
}
__getInlineStyles() {
const { assetPrefix } = this.context;
const files: DocumentFiles = getDocumentFiles(this.context.buildManifest, this.context.__NEXT_DATA__.page);
if (!files || files.allFiles.length === 0) {
return null;
}
return files.allFiles
.filter(f => f.endsWith('.css'))
.map(file => (
<style
key={file}
nonce={this.props.nonce}
data-href={`${assetPrefix}/_next/${file}`}
dangerouslySetInnerHTML={{
__html: readFileSync(resolve(process.cwd(), '.next', file), 'utf-8'),
}}
/>
));
}
} |
Beta Was this translation helpful? Give feedback.
Because nobody of the nextjs core team answered my question: Here is our workaround. It's the same impl. @Timer used in the related PR.