Skip to content

Commit 4d50072

Browse files
committed
Document #428
1 parent 532ad76 commit 4d50072

File tree

1 file changed

+54
-4
lines changed

1 file changed

+54
-4
lines changed

template/README.md

+54-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ You can find the most recent version of this guide [here](https://github.com/fac
2424
- [Adding Custom Environment Variables](#adding-custom-environment-variables)
2525
- [Integrating with a Node Backend](#integrating-with-a-node-backend)
2626
- [Proxying API Requests in Development](#proxying-api-requests-in-development)
27-
- [Adding `<meta>` Tags](#adding-meta-tags)
27+
- [Adding `<link>` and `<meta>` Tags](#adding-link-and-meta-tags)
28+
- [Referring to Static Assets from `<link href>`](#referring-to-static-assets-from-link-href)
29+
- [Generating Dynamic `<meta>` Tags on the Server](#generating-dynamic-meta-tags-on-the-server)
2830
- [Running Tests](#running-tests)
2931
- [Filename Conventions](#filename-conventions)
3032
- [Command Line Interface](#command-line-interface)
@@ -525,11 +527,59 @@ If the `proxy` option is **not** flexible enough for you, alternatively you can:
525527
* Enable CORS on your server ([here’s how to do it for Express](http://enable-cors.org/server_expressjs.html)).
526528
* Use [environment variables](#adding-custom-environment-variables) to inject the right server host and port into your app.
527529
528-
## Adding `<meta>` Tags
530+
## Adding `<link>` and `<meta>` Tags
529531
530-
You can edit the generated `index.html` and add any tags you’d like to it. However, since Create React App doesn’t support server rendering, you might be wondering how to make `<meta>` tags dynamic and reflect the current URL.
532+
You can edit the generated `index.html` and add any tags you’d like to it.
531533
532-
To solve this, we recommend to add placeholders into the HTML, like this:
534+
### Referring to Static Assets from `<link href>`
535+
536+
>Note: this feature is available with `react-scripts@0.3.0` and higher.
537+
538+
Sometimes, you might want to refer to static assets from `index.html`. Create React App intentionally does not support serving static assets from a folder because it is too easy to forget to arrange cache invalidation for their filenames. Instead, we recommend that all assets are [handled as part of build process with `import`s](#adding-images-and-fonts).
539+
540+
However, you can’t `import` anything from an HTML file. This is why Create React App automatically treats any `<link href>` attributes that start with `./` as a hint that this file needs to be included in the build process. For example, you can use paths like this in `index.html`:
541+
542+
```html
543+
<link rel="shortcut icon" href="./src/favicon.ico">
544+
<link rel="icon" href="./src/favicon/favicon-16.png" sizes="16x16" type="image/png">
545+
<link rel="icon" href="./src/favicon/favicon-32.png" sizes="32x32" type="image/png">
546+
<link rel="icon" href="./src/favicon/favicon-64.png" sizes="64x64" type="image/png">
547+
```
548+
549+
Webpack will parse those `<link href>` attributes and replace them with real paths.
550+
In production, they will become:
551+
552+
```html
553+
<link rel="shortcut icon" href="/favicon.ico?fd73a6eb">
554+
<link rel="icon" href="/static/media/favicon-16.06a6e0a8.png" sizes="16x16" type="image/png">
555+
<link rel="icon" href="/static/media/favicon-32.eb28da34.png" sizes="32x32" type="image/png">
556+
<link rel="icon" href="/static/media/favicon-64.91cb3479.png" sizes="64x64" type="image/png">
557+
```
558+
559+
For this to work, **make sure to specify paths relatively** so don’t forget the `./`:
560+
561+
```html
562+
<!-- Will be resolved by Webpack on build to the real file. -->
563+
<!-- Use this in most cases: -->
564+
<link rel="icon" href="./src/favicon/favicon-32.png" sizes="32x32" type="image/png">
565+
<!-- See the ./ here: ^^^ -->
566+
567+
<!-- Will actually request http://yourserver.com/src/favicon/favicon-32.png. -->
568+
<!-- Only use this if you know this file will appear on your server and is *not* part of your build: -->
569+
<link rel="icon" href="/src/favicon/favicon-32.png" sizes="32x32" type="image/png">
570+
```
571+
572+
Files starting with `./` in `<link href>` attribute will be copied to the `static` folder inside your `build` output, and HTML will reference them instead. Webpack will throw a compilation error if any of these files was accidentally deleted or misspelled.
573+
574+
Their names will also contain the content hashes to make sure the browser cache is busted when the file changes. The only file that is handled specially is `favicon.ico` which, if present and referenced from HTML, will be always placed at the root so that browsers can find it even when requesting files from the server (such as PDF documents).
575+
576+
Currently, only `<link href>` attributes are treated this way. If you need similar support for other HTML tags and attributes, please file an issue describing your use case.
577+
578+
If you need to use an asset from code rather than from HTML, please read [Adding Images and Fonts](#adding-images-and-fonts).
579+
580+
### Generating Dynamic `<meta>` Tags on the Server
581+
582+
Since Create React App doesn’t support server rendering, you might be wondering how to make `<meta>` tags dynamic and reflect the current URL. To solve this, we recommend to add placeholders into the HTML, like this:
533583
534584
```html
535585
<!doctype html>

0 commit comments

Comments
 (0)