Skip to content

Commit

Permalink
Merge branch 'canary' into app/allow-empty-pages-folder
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Sep 1, 2022
2 parents f7a80dd + 2de7b43 commit eb7efe4
Show file tree
Hide file tree
Showing 185 changed files with 1,683 additions and 699 deletions.
84 changes: 38 additions & 46 deletions docs/api-reference/next/future/image.md
@@ -1,34 +1,23 @@
---
description: Try the latest Image Optimization with the experimental `next/future/image` component.
description: Try the latest Image Optimization with the new `next/future/image` component.
---

# next/future/image

<details>
<summary><b>Version History</b></summary>

| Version | Changes |
| --------- | -------------------------------------------- |
| `v12.2.4` | Support for `fill` property added. |
| `v12.2.0` | Experimental `next/future/image` introduced. |
| Version | Changes |
| --------- | --------------------------------------------------------------------------------------------------------------------------- |
| `v12.3.0` | `next/future/image` component stable. `remotePatterns` config stable. `unoptimized` config stable. `alt` property required. |
| `v12.2.4` | `fill` property added. |
| `v12.2.0` | Experimental `next/future/image` component introduced. |

</details>

The `next/future/image` component is an experiment to improve both the performance and developer experience of `next/image` by using the native `<img>` element with better default behavior.
The `next/future/image` component improves both the performance and developer experience of `next/image` by using the native `<img>` element with better default behavior.

This new component is considered experimental and therefore not covered by semver, and may cause unexpected or broken application behavior. This component uses browser native [lazy loading](https://caniuse.com/loading-lazy-attr), which may fallback to eager loading for older browsers before Safari 15.4. When using the blur-up placeholder, older browsers before Safari 12 will fallback to empty placeholder. When using styles with `width`/`height` of `auto`, it is possible to cause [Layout Shift](https://web.dev/cls/) on older browsers before Safari 15 that don't [preserve the aspect ratio](https://caniuse.com/mdn-html_elements_img_aspect_ratio_computed_from_attributes). For more details, see [this MDN video](https://www.youtube.com/watch?v=4-d_SoCHeWE).

To use `next/future/image`, add the following to your `next.config.js` file:

```js
module.exports = {
experimental: {
images: {
allowFutureImage: true,
},
},
}
```
This component uses browser native [lazy loading](https://caniuse.com/loading-lazy-attr), which may fallback to eager loading for older browsers before Safari 15.4. When using the blur-up placeholder, older browsers before Safari 12 will fallback to empty placeholder. When using styles with `width`/`height` of `auto`, it is possible to cause [Layout Shift](https://web.dev/cls/) on older browsers before Safari 15 that don't [preserve the aspect ratio](https://caniuse.com/mdn-html_elements_img_aspect_ratio_computed_from_attributes). For more details, see [this MDN video](https://www.youtube.com/watch?v=4-d_SoCHeWE).

## Comparison

Expand All @@ -43,6 +32,7 @@ Compared to `next/image`, the new `next/future/image` component has the followin
- Removes `lazyBoundary` prop since there is no native equivalent
- Removes `lazyRoot` prop since there is no native equivalent
- Removes `loader` config in favor of [`loader`](#loader) prop
- Changed `alt` prop from optional to required

## Known Browser Bugs

Expand Down Expand Up @@ -175,6 +165,16 @@ The `height` property represents the _rendered_ height in pixels, so it will aff

Required, except for [statically imported images](/docs/basic-features/image-optimization.md#local-images) or images with the [`fill` property](#fill).

### alt

The `alt` property is used to describe the image for screen readers and search engines. It is also the fallback text if images have been disabled or an error occurs while loading the image.

It should contain text that could replace the image [without changing the meaning of the page](https://html.spec.whatwg.org/multipage/images.html#general-guidelines). It is not meant to supplement the image and should not repeat information that is already provided in the captions above or below the image.

If the image is [purely decorative](https://html.spec.whatwg.org/multipage/images.html#a-purely-decorative-image-that-doesn't-add-any-information) or [not intended for the user](https://html.spec.whatwg.org/multipage/images.html#an-image-not-intended-for-the-user), the `alt` property should be an empty string (`alt=""`).

[Learn more](https://html.spec.whatwg.org/multipage/images.html#alt)

## Optional Props

The `<Image />` component accepts a number of additional properties beyond those which are required. This section describes the most commonly-used properties of the Image component. Find details about more rarely-used properties in the [Advanced Props](#advanced-props) section.
Expand Down Expand Up @@ -362,14 +362,12 @@ You can also [generate a solid color Data URL](https://png-pixel.com) to match t
When true, the source image will be served as-is instead of changing quality,
size, or format. Defaults to `false`.

This prop can be assigned to all images by updating `next.config.js` with the following experimental configuration:
This prop can be assigned to all images by updating `next.config.js` with the following configuration:

```js
module.exports = {
experimental: {
images: {
unoptimized: true,
},
images: {
unoptimized: true,
},
}
```
Expand All @@ -387,23 +385,19 @@ Other properties on the `<Image />` component will be passed to the underlying

### Remote Patterns

> Note: The `remotePatterns` configuration is currently **experimental** and subject to change. Please use [`domains`](#domains) for production use cases.
To protect your application from malicious users, configuration is required in order to use external images. This ensures that only external images from your account can be served from the Next.js Image Optimization API. These external images can be configured with the `remotePatterns` property in your `next.config.js` file, as shown below:

```js
module.exports = {
experimental: {
images: {
remotePatterns: [
{
protocol: 'https',
hostname: 'example.com',
port: '',
pathname: '/account123/**',
},
],
},
images: {
remotePatterns: [
{
protocol: 'https',
hostname: 'example.com',
port: '',
pathname: '/account123/**',
},
],
},
}
```
Expand All @@ -414,15 +408,13 @@ Below is another example of the `remotePatterns` property in the `next.config.js

```js
module.exports = {
experimental: {
images: {
remotePatterns: [
{
protocol: 'https',
hostname: '**.example.com',
},
],
},
images: {
remotePatterns: [
{
protocol: 'https',
hostname: '**.example.com',
},
],
},
}
```
Expand Down
49 changes: 20 additions & 29 deletions docs/api-reference/next/image.md
Expand Up @@ -16,6 +16,7 @@ description: Enable Image Optimization with the built-in Image component.

| Version | Changes |
| --------- | --------------------------------------------------------------------------------------------------------- |
| `v12.3.0` | `remotePatterns` and `unoptimized` configuration is stable. |
| `v12.2.0` | Experimental `remotePatterns` and experimental `unoptimized` configuration added. `layout="raw"` removed. |
| `v12.1.1` | `style` prop added. Experimental[\*](#experimental-raw-layout-mode) support for `layout="raw"` added. |
| `v12.1.0` | `dangerouslyAllowSVG` and `contentSecurityPolicy` configuration added. |
Expand Down Expand Up @@ -93,8 +94,6 @@ The layout behavior of the image as the viewport changes size.
- When `fill`, the image will stretch both width and height to the dimensions of the parent element, provided the parent element is relative.
- This is usually paired with the [`objectFit`](#objectFit) property.
- Ensure the parent element has `position: relative` in their stylesheet.
- When `raw`[\*](#experimental-raw-layout-mode), the image will be rendered as a single image element with no wrappers, sizers or other responsive behavior.
- If your image styling will change the size of a `raw` image, you should include the `sizes` property for proper image serving. Otherwise your image will be requested as though it has fixed width and height.
- [Demo background image](https://image-component.nextjs.gallery/background)

### loader
Expand Down Expand Up @@ -324,14 +323,12 @@ const Example = () => {
When true, the source image will be served as-is instead of changing quality,
size, or format. Defaults to `false`.

This prop can be assigned to all images by updating `next.config.js` with the following experimental configuration:
This prop can be assigned to all images by updating `next.config.js` with the following configuration:

```js
module.exports = {
experimental: {
images: {
unoptimized: true,
},
images: {
unoptimized: true,
},
}
```
Expand All @@ -351,23 +348,19 @@ Other properties on the `<Image />` component will be passed to the underlying

### Remote Patterns

> Note: The `remotePatterns` configuration is currently **experimental** and subject to change. Please use [`domains`](#domains) for production use cases.
To protect your application from malicious users, configuration is required in order to use external images. This ensures that only external images from your account can be served from the Next.js Image Optimization API. These external images can be configured with the `remotePatterns` property in your `next.config.js` file, as shown below:

```js
module.exports = {
experimental: {
images: {
remotePatterns: [
{
protocol: 'https',
hostname: 'example.com',
port: '',
pathname: '/account123/**',
},
],
},
images: {
remotePatterns: [
{
protocol: 'https',
hostname: 'example.com',
port: '',
pathname: '/account123/**',
},
],
},
}
```
Expand All @@ -378,15 +371,13 @@ Below is another example of the `remotePatterns` property in the `next.config.js

```js
module.exports = {
experimental: {
images: {
remotePatterns: [
{
protocol: 'https',
hostname: '**.example.com',
},
],
},
images: {
remotePatterns: [
{
protocol: 'https',
hostname: '**.example.com',
},
],
},
}
```
Expand Down
38 changes: 17 additions & 21 deletions docs/basic-features/font-optimization.md
Expand Up @@ -27,28 +27,24 @@ To add a web font to your Next.js application, add the font to a [Custom `Docume
```js
// pages/_document.js

import Document, { Html, Head, Main, NextScript } from 'next/document'

class MyDocument extends Document {
render() {
return (
<Html>
<Head>
<link
href="https://fonts.googleapis.com/css2?family=Inter&display=optional"
rel="stylesheet"
/>
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
import { Html, Head, Main, NextScript } from 'next/document'

export default function Document() {
return (
<Html>
<Head>
<link
href="https://fonts.googleapis.com/css2?family=Inter&display=optional"
rel="stylesheet"
/>
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
)
}

export default MyDocument
```
Adding fonts to `_document` is preferred over individual pages. When adding fonts to a single page with [`next/head`](/docs/api-reference/next/head.md), font optimizations included by Next.js will not work on navigations between pages client-side or when using [streaming](/docs/advanced-features/react-18/streaming.md).
Expand Down
2 changes: 1 addition & 1 deletion docs/manifest.json
Expand Up @@ -427,7 +427,7 @@
}
},
{
"title": "next/future/image (experimental)",
"title": "next/future/image",
"path": "/docs/api-reference/next/future/image.md"
},
{
Expand Down
15 changes: 4 additions & 11 deletions errors/invalid-images-config.md
Expand Up @@ -31,17 +31,10 @@ module.exports = {
dangerouslyAllowSVG: false,
// set the Content-Security-Policy header
contentSecurityPolicy: "default-src 'self'; script-src 'none'; sandbox;",
// the following are experimental features, and may cause breaking changes
},
experimental: {
images: {
// limit of 50 objects
remotePatterns: [],
// when true, every image will be unoptimized
unoptimized: false,
// when true, allow `next/future/image` to be imported
allowFutureImage: false,
},
// limit of 50 objects
remotePatterns: [],
// when true, every image will be unoptimized
unoptimized: false,
},
}
```
Expand Down
1 change: 1 addition & 0 deletions examples/active-class-name/next.config.js
@@ -1,3 +1,4 @@
/** @type {import('next').NextConfig} */
module.exports = {
async rewrites() {
return [
Expand Down
2 changes: 1 addition & 1 deletion examples/analyze-bundles/next.config.js
@@ -1,8 +1,8 @@
/** @type {import('next').NextConfig} */
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true',
})

/** @type {import('next').NextConfig} */
const nextConfig = {
// any configs you need
}
Expand Down
1 change: 1 addition & 0 deletions examples/blog-starter/tailwind.config.js
@@ -1,3 +1,4 @@
/** @type {import('tailwindcss').Config} */
module.exports = {
content: ['./components/**/*.tsx', './pages/**/*.tsx'],
theme: {
Expand Down
1 change: 1 addition & 0 deletions examples/blog-with-comment/tailwind.config.js
@@ -1,3 +1,4 @@
/** @type {import('tailwindcss').Config} */
module.exports = {
mode: 'jit',
purge: ['./pages/**/*.js', './components/**/*.js'],
Expand Down
2 changes: 1 addition & 1 deletion examples/blog/next.config.js
@@ -1,10 +1,10 @@
/** @type {import('next').NextConfig} */
const withNextra = require('nextra')({
theme: 'nextra-theme-blog',
themeConfig: './theme.config.js',
// optional: add `unstable_staticImage: true` to enable Nextra's auto image import
})

/** @type {import('next').NextConfig} */
const nextConfig = {
// any configs you need
}
Expand Down
1 change: 1 addition & 0 deletions examples/cms-agilitycms/tailwind.config.js
@@ -1,3 +1,4 @@
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
'./pages/**/*.{js,ts,jsx,tsx}',
Expand Down
1 change: 1 addition & 0 deletions examples/cms-builder-io/next.config.js
@@ -1,3 +1,4 @@
/** @type {import('next').NextConfig} */
module.exports = {
images: {
domains: ['cdn.builder.io'],
Expand Down
1 change: 1 addition & 0 deletions examples/cms-builder-io/tailwind.config.js
@@ -1,3 +1,4 @@
/** @type {import('tailwindcss').Config} */
module.exports = {
content: ['./components/**/*.js', './pages/**/*.js'],
theme: {
Expand Down
1 change: 1 addition & 0 deletions examples/cms-buttercms/next.config.js
@@ -1,3 +1,4 @@
/** @type {import('next').NextConfig} */
module.exports = {
reactStrictMode: true,
async rewrites() {
Expand Down
1 change: 1 addition & 0 deletions examples/cms-contentful/next.config.js
@@ -1,3 +1,4 @@
/** @type {import('next').NextConfig} */
module.exports = {
images: {
loader: 'custom',
Expand Down
1 change: 1 addition & 0 deletions examples/cms-contentful/tailwind.config.js
@@ -1,3 +1,4 @@
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
'./pages/**/*.{js,ts,jsx,tsx}',
Expand Down
1 change: 1 addition & 0 deletions examples/cms-cosmic/next.config.js
@@ -1,3 +1,4 @@
/** @type {import('next').NextConfig} */
module.exports = {
images: {
domains: ['imgix.cosmicjs.com'],
Expand Down
1 change: 1 addition & 0 deletions examples/cms-cosmic/tailwind.config.js
@@ -1,3 +1,4 @@
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
'./pages/**/*.{js,ts,jsx,tsx}',
Expand Down
1 change: 1 addition & 0 deletions examples/cms-datocms/next.config.js
@@ -1,3 +1,4 @@
/** @type {import('next').NextConfig} */
module.exports = {
images: {
domains: ['www.datocms-assets.com'],
Expand Down
1 change: 1 addition & 0 deletions examples/cms-datocms/tailwind.config.js
@@ -1,3 +1,4 @@
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
'./pages/**/*.{js,ts,jsx,tsx}',
Expand Down

0 comments on commit eb7efe4

Please sign in to comment.