elements but should be specified otherwise.\nExamples\nResponsive", "source": "https://nextjs.org/docs/app/building-your-application/optimizing/images"}
{"id": "f6dc9a7dd197-10", "text": "This is the default for
elements but should be specified otherwise.\nExamples\nResponsive\n import Image from 'next/image'\nimport mountains from '../public/mountains.jpg'\n \nexport default function Responsive() {\n return (\n
\n \n
\n )\n}\nFill Container\n import Image from 'next/image'\nimport mountains from '../public/mountains.jpg'\n \nexport default function Fill() {\n return (\n
\n
\n \n
\n {/* And more images in the grid... */}\n
\n )\n}\nBackground Image\n import Image from 'next/image'\nimport mountains from '../public/mountains.jpg'\n \nexport default function Background() {", "source": "https://nextjs.org/docs/app/building-your-application/optimizing/images"}
{"id": "f6dc9a7dd197-12", "text": "import mountains from '../public/mountains.jpg'\n \nexport default function Background() {\n return (\n
\n )\n}\nFor examples of the Image component used with the various styles, see the Image Component Demo.\nOther Properties\nView all properties available to the next/image component.\nConfiguration\nThe next/image component and Next.js Image Optimization API can be configured in the next.config.js file. These configurations allow you to enable remote images, define custom image breakpoints, change caching behavior and more.\nRead the full image configuration documentation for more information.", "source": "https://nextjs.org/docs/app/building-your-application/optimizing/images"}
{"id": "43a365f70a81-0", "text": "Font Optimization\nnext/font will automatically optimize your fonts (including custom fonts) and remove external network requests for improved privacy and performance.\n\ud83c\udfa5 Watch: Learn more about how to use next/font \u2192 YouTube (6 minutes).\nnext/font includes built-in automatic self-hosting for any font file. This means you can optimally load web fonts with zero layout shift, thanks to the underlying CSS size-adjust property used.\nThis new font system also allows you to conveniently use all Google Fonts with performance and privacy in mind. CSS and font files are downloaded at build time and self-hosted with the rest of your static assets. No requests are sent to Google by the browser.\nGoogle Fonts\nAutomatically self-host any Google Font. Fonts are included in the deployment and served from the same domain as your deployment. No requests are sent to Google by the browser.", "source": "https://nextjs.org/docs/app/building-your-application/optimizing/fonts"}
{"id": "43a365f70a81-1", "text": "Get started by importing the font you would like to use from next/font/google as a function. We recommend using variable fonts for the best performance and flexibility.\napp/layout.tsx import { Inter } from 'next/font/google'\n \n// If loading a variable font, you don't need to specify the font weight\nconst inter = Inter({\n subsets: ['latin'],\n display: 'swap',\n})\n \nexport default function RootLayout({\n children,\n}: {\n children: React.ReactNode\n}) {\n return (\n \n {children}\n \n )\n}If you can't use a variable font, you will need to specify a weight:app/layout.tsx import { Roboto } from 'next/font/google'\n \nconst roboto = Roboto({\n weight: '400',", "source": "https://nextjs.org/docs/app/building-your-application/optimizing/fonts"}
{"id": "43a365f70a81-2", "text": "const roboto = Roboto({\n weight: '400',\n subsets: ['latin'],\n display: 'swap',\n})\n \nexport default function RootLayout({\n children,\n}: {\n children: React.ReactNode\n}) {\n return (\n \n {children}\n \n )\n}\nYou can specify multiple weights and/or styles by using an array:\napp/layout.js const roboto = Roboto({\n weight: ['400', '700'],\n style: ['normal', 'italic'],\n subsets: ['latin'],\n display: 'swap',\n})\nGood to know: Use an underscore (_) for font names with multiple words. E.g. Roboto Mono should be imported as Roboto_Mono.\nSpecifying a subset", "source": "https://nextjs.org/docs/app/building-your-application/optimizing/fonts"}
{"id": "43a365f70a81-3", "text": "Specifying a subset\nGoogle Fonts are automatically subset. This reduces the size of the font file and improves performance. You'll need to define which of these subsets you want to preload. Failing to specify any subsets while preload is true will result in a warning.\nThis can be done by adding it to the function call:\napp/layout.tsx const inter = Inter({ subsets: ['latin'] })\nView the Font API Reference for more information.\nUsing Multiple Fonts\nYou can import and use multiple fonts in your application. There are two approaches you can take.\nThe first approach is to create a utility function that exports a font, imports it, and applies its className where needed. This ensures the font is preloaded only when it's rendered:\napp/fonts.ts import { Inter, Roboto_Mono } from 'next/font/google'\n \nexport const inter = Inter({\n subsets: ['latin'],\n display: 'swap',\n})", "source": "https://nextjs.org/docs/app/building-your-application/optimizing/fonts"}
{"id": "43a365f70a81-4", "text": "subsets: ['latin'],\n display: 'swap',\n})\n \nexport const roboto_mono = Roboto_Mono({\n subsets: ['latin'],\n display: 'swap',\n})\napp/layout.tsx import { inter } from './fonts'\n \nexport default function Layout({ children }: { children: React.ReactNode }) {\n return (\n \n \n
{children}
\n \n \n )\n}app/page.tsx import { roboto_mono } from './fonts'\n \nexport default function Page() {\n return (\n <>\n
My page
\n >\n )\n}", "source": "https://nextjs.org/docs/app/building-your-application/optimizing/fonts"}
{"id": "43a365f70a81-5", "text": ">\n )\n}\nIn the example above, Inter will be applied globally, and Roboto Mono can be imported and applied as needed.\nAlternatively, you can create a CSS variable and use it with your preferred CSS solution:\napp/layout.tsx import { Inter, Roboto_Mono } from 'next/font/google'\nimport styles from './global.css'\n \nconst inter = Inter({\n subsets: ['latin'],\n variable: '--font-inter',\n display: 'swap',\n})\n \nconst roboto_mono = Roboto_Mono({\n subsets: ['latin'],\n variable: '--font-roboto-mono',\n display: 'swap',\n})\n \nexport default function RootLayout({\n children,\n}: {\n children: React.ReactNode\n}) {\n return (", "source": "https://nextjs.org/docs/app/building-your-application/optimizing/fonts"}
{"id": "43a365f70a81-6", "text": "children,\n}: {\n children: React.ReactNode\n}) {\n return (\n \n \n
My App
\n
{children}
\n \n \n )\n}\napp/global.css html {\n font-family: var(--font-inter);\n}\n \nh1 {\n font-family: var(--font-roboto-mono);\n}\nIn the example above, Inter will be applied globally, and any
tags will be styled with Roboto Mono.\nRecommendation: Use multiple fonts conservatively since each new font is an additional resource the client has to download.\nLocal Fonts", "source": "https://nextjs.org/docs/app/building-your-application/optimizing/fonts"}
{"id": "43a365f70a81-7", "text": "Local Fonts\nImport next/font/local and specify the src of your local font file. We recommend using variable fonts for the best performance and flexibility.\napp/layout.tsx import localFont from 'next/font/local'\n \n// Font files can be colocated inside of `app`\nconst myFont = localFont({\n src: './my-font.woff2',\n display: 'swap',\n})\n \nexport default function RootLayout({\n children,\n}: {\n children: React.ReactNode\n}) {\n return (\n \n {children}\n \n )\n}\nIf you want to use multiple files for a single font family, src can be an array:\n const roboto = localFont({\n src: [\n {\n path: './Roboto-Regular.woff2',", "source": "https://nextjs.org/docs/app/building-your-application/optimizing/fonts"}
{"id": "43a365f70a81-8", "text": "src: [\n {\n path: './Roboto-Regular.woff2',\n weight: '400',\n style: 'normal',\n },\n {\n path: './Roboto-Italic.woff2',\n weight: '400',\n style: 'italic',\n },\n {\n path: './Roboto-Bold.woff2',\n weight: '700',\n style: 'normal',\n },\n {\n path: './Roboto-BoldItalic.woff2',\n weight: '700',\n style: 'italic',\n },\n ],\n})\nView the Font API Reference for more information.\nWith Tailwind CSS\nnext/font can be used with Tailwind CSS through a CSS variable.", "source": "https://nextjs.org/docs/app/building-your-application/optimizing/fonts"}
{"id": "43a365f70a81-9", "text": "With Tailwind CSS\nnext/font can be used with Tailwind CSS through a CSS variable.\nIn the example below, we use the font Inter from next/font/google (you can use any font from Google or Local Fonts). Load your font with the variable option to define your CSS variable name and assign it to inter. Then, use inter.variable to add the CSS variable to your HTML document.\napp/layout.tsx import { Inter, Roboto_Mono } from 'next/font/google'\n \nconst inter = Inter({\n subsets: ['latin'],\n display: 'swap',\n variable: '--font-inter',\n})\n \nconst roboto_mono = Roboto_Mono({\n subsets: ['latin'],\n display: 'swap',\n variable: '--font-roboto-mono',\n})\n \nexport default function RootLayout({\n children,\n}: {\n children: React.ReactNode\n}) {", "source": "https://nextjs.org/docs/app/building-your-application/optimizing/fonts"}
{"id": "43a365f70a81-10", "text": "children,\n}: {\n children: React.ReactNode\n}) {\n return (\n \n {children}\n \n )\n}\nFinally, add the CSS variable to your Tailwind CSS config:\ntailwind.config.js /** @type {import('tailwindcss').Config} */\nmodule.exports = {\n content: [\n './pages/**/*.{js,ts,jsx,tsx}',\n './components/**/*.{js,ts,jsx,tsx}',\n './app/**/*.{js,ts,jsx,tsx}',\n ],\n theme: {\n extend: {\n fontFamily: {\n sans: ['var(--font-inter)'],\n mono: ['var(--font-roboto-mono)'],\n },\n },\n },", "source": "https://nextjs.org/docs/app/building-your-application/optimizing/fonts"}
{"id": "43a365f70a81-11", "text": "},\n },\n },\n plugins: [],\n}\nYou can now use the font-sans and font-mono utility classes to apply the font to your elements.\nPreloading\nWhen a font function is called on a page of your site, it is not globally available and preloaded on all routes. Rather, the font is only preloaded on the related routes based on the type of file where it is used:\nIf it's a unique page, it is preloaded on the unique route for that page.\nIf it's a layout, it is preloaded on all the routes wrapped by the layout.\nIf it's the root layout, it is preloaded on all routes.\nReusing fonts", "source": "https://nextjs.org/docs/app/building-your-application/optimizing/fonts"}
{"id": "43a365f70a81-12", "text": "If it's the root layout, it is preloaded on all routes.\nReusing fonts\nEvery time you call the localFont or Google font function, that font is hosted as one instance in your application. Therefore, if you load the same font function in multiple files, multiple instances of the same font are hosted. In this situation, it is recommended to do the following:\nCall the font loader function in one shared file\nExport it as a constant\nImport the constant in each file where you would like to use this font", "source": "https://nextjs.org/docs/app/building-your-application/optimizing/fonts"}
{"id": "995394ed8592-0", "text": "Script Optimization\nLayout Scripts\nTo load a third-party script for multiple routes, import next/script and include the script directly in your layout component:app/dashboard/layout.tsx import Script from 'next/script'\n \nexport default function DashboardLayout({\n children,\n}: {\n children: React.ReactNode\n}) {\n return (\n <>\n \n \n >\n )\n}The third-party script is fetched when the folder route (e.g. dashboard/page.js) or any nested route (e.g. dashboard/settings/page.js) is accessed by the user. Next.js will ensure the script will only load once, even if a user navigates between multiple routes in the same layout.\nApplication Scripts", "source": "https://nextjs.org/docs/app/building-your-application/optimizing/scripts"}
{"id": "995394ed8592-1", "text": "Application Scripts\nTo load a third-party script for all routes, import next/script and include the script directly in your root layout:app/layout.tsx import Script from 'next/script'\n \nexport default function RootLayout({\n children,\n}: {\n children: React.ReactNode\n}) {\n return (\n \n {children}\n \n \n )\n}\nThis script will load and execute when any route in your application is accessed. Next.js will ensure the script will only load once, even if a user navigates between multiple pages.\nRecommendation: We recommend only including third-party scripts in specific pages or layouts in order to minimize any unnecessary impact to performance.\nStrategy", "source": "https://nextjs.org/docs/app/building-your-application/optimizing/scripts"}
{"id": "995394ed8592-2", "text": "Strategy\nAlthough the default behavior of next/script allows you load third-party scripts in any page or layout, you can fine-tune its loading behavior by using the strategy property:\nbeforeInteractive: Load the script before any Next.js code and before any page hydration occurs.\nafterInteractive: (default) Load the script early but after some hydration on the page occurs.\nlazyOnload: Load the script later during browser idle time.\nworker: (experimental) Load the script in a web worker.\nRefer to the next/script API reference documentation to learn more about each strategy and their use cases.\nOffloading Scripts To A Web Worker (Experimental)\nWarning: The worker strategy is not yet stable and does not yet work with the app directory. Use with caution.\nScripts that use the worker strategy are offloaded and executed in a web worker with Partytown. This can improve the performance of your site by dedicating the main thread to the rest of your application code.", "source": "https://nextjs.org/docs/app/building-your-application/optimizing/scripts"}
{"id": "995394ed8592-3", "text": "This strategy is still experimental and can only be used if the nextScriptWorkers flag is enabled in next.config.js:\nnext.config.js module.exports = {\n experimental: {\n nextScriptWorkers: true,\n },\n}\nThen, run next (normally npm run dev or yarn dev) and Next.js will guide you through the installation of the required packages to finish the setup:\nTerminal npm run dev\nYou'll see instructions like these: Please install Partytown by running npm install @builder.io/partytown\nOnce setup is complete, defining strategy=\"worker\" will automatically instantiate Partytown in your application and offload the script to a web worker.\npages/home.tsx import Script from 'next/script'\n \nexport default function Home() {\n return (\n <>\n \n >\n )\n}", "source": "https://nextjs.org/docs/app/building-your-application/optimizing/scripts"}
{"id": "995394ed8592-4", "text": ">\n )\n}\nThere are a number of trade-offs that need to be considered when loading a third-party script in a web worker. Please see Partytown's tradeoffs documentation for more information.\nInline Scripts\nInline scripts, or scripts not loaded from an external file, are also supported by the Script component. They can be written by placing the JavaScript within curly braces:\n \nOr by using the dangerouslySetInnerHTML property:\n \nWarning: An id property must be assigned for inline scripts in order for Next.js to track and optimize the script.\nExecuting Additional Code", "source": "https://nextjs.org/docs/app/building-your-application/optimizing/scripts"}
{"id": "995394ed8592-5", "text": "Executing Additional Code\nEvent handlers can be used with the Script component to execute additional code after a certain event occurs:\nonLoad: Execute code after the script has finished loading.\nonReady: Execute code after the script has finished loading and every time the component is mounted.\nonError: Execute code if the script fails to load.\nThese handlers will only work when next/script is imported and used inside of a Client Component where \"use client\" is defined as the first line of code:app/page.tsx 'use client'\n \nimport Script from 'next/script'\n \nexport default function Page() {\n return (\n <>\n