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

Option to preload fonts #4039

Closed
benmccann opened this issue Feb 22, 2022 · 1 comment · Fixed by #4963
Closed

Option to preload fonts #4039

benmccann opened this issue Feb 22, 2022 · 1 comment · Fixed by #4963
Labels
feature / enhancement New feature or request
Milestone

Comments

@benmccann
Copy link
Member

Describe the problem

For performance, you probably want to either preload fonts or swap them

Describe the proposed solution

There's an ssrManifest option that can be passed to the client build that we're not using today. It would generate an ssr-manifest.json which includes the font files

Alternatives considered

No response

Importance

nice to have

Additional Information

No response

@Rich-Harris Rich-Harris added the feature / enhancement New feature or request label Mar 2, 2022
@Rich-Harris Rich-Harris added this to the 1.0 milestone May 16, 2022
@Grsmto
Copy link

Grsmto commented Oct 21, 2022

For anyone reading this, this is how I did it in the meanwhile:

  1. Create a small plugin to not hash the font files (otherwise we can't reference them in our html)
function VitePluginRemoveHashFontFiles() {
	return {
		name: 'Remove hash from font files',
		apply: 'build',
		enforce: 'post',
		config(config) {
			const defaultAssetFileNames = config.build.rollupOptions.output.assetFileNames;
			config.build.rollupOptions.output.assetFileNames = ({ type, name }) => {
				if (type === 'asset' && /\.(woff2?|ttf|otf)$/.test(name)) return `[name].[ext]`;
				return defaultAssetFileNames;
			};
		}
	};
}

const config = {
	plugins: [sveltekit(), VitePluginRenameCSSFontFiles()]
};
  1. Then you can add your preload link somewhere in your app
<svelte:head>
	<link
		rel="preload"
		href="/open-sans-latin-variable-wghtOnly-italic.woff2"
		as="font"
		crossOrigin="anonymous"
	/>
</svelte:head>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature / enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants