Skip to content

Commit 54615ee

Browse files
authored
Explain how to use #703
1 parent fda91eb commit 54615ee

File tree

1 file changed

+59
-63
lines changed

1 file changed

+59
-63
lines changed

packages/react-scripts/template/README.md

+59-63
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@ You can find the most recent version of this guide [here](https://github.com/fac
1919
- [Adding a Stylesheet](#adding-a-stylesheet)
2020
- [Post-Processing CSS](#post-processing-css)
2121
- [Adding Images and Fonts](#adding-images-and-fonts)
22+
- [Using the `public` Folder](#using-the-public-folder)
2223
- [Adding Bootstrap](#adding-bootstrap)
2324
- [Adding Flow](#adding-flow)
2425
- [Adding Custom Environment Variables](#adding-custom-environment-variables)
2526
- [Can I Use Decorators?](#can-i-use-decorators)
2627
- [Integrating with a Node Backend](#integrating-with-a-node-backend)
2728
- [Proxying API Requests in Development](#proxying-api-requests-in-development)
2829
- [Using HTTPS in Development](#using-https-in-development)
29-
- [Adding `<link>` and `<meta>` Tags](#adding-link-and-meta-tags)
30-
- [Referring to Static Assets from `<link href>`](#referring-to-static-assets-from-link-href)
31-
- [Generating Dynamic `<meta>` Tags on the Server](#generating-dynamic-meta-tags-on-the-server)
30+
- [Generating Dynamic `<meta>` Tags on the Server](#generating-dynamic-meta-tags-on-the-server)
3231
- [Running Tests](#running-tests)
3332
- [Filename Conventions](#filename-conventions)
3433
- [Command Line Interface](#command-line-interface)
@@ -79,30 +78,33 @@ After creation, your project should look like this:
7978
```
8079
my-app/
8180
README.md
82-
index.html
8381
node_modules/
8482
package.json
83+
public/
84+
index.html
85+
favicon.ico
8586
src/
8687
App.css
8788
App.js
8889
App.test.js
89-
favicon.ico
9090
index.css
9191
index.js
9292
logo.svg
9393
```
9494

9595
For the project to build, **these files must exist with exact filenames**:
9696

97-
* `index.html` is the page template;
98-
* `src/favicon.ico` is the icon you see in the browser tab;
97+
* `public/index.html` is the page template;
9998
* `src/index.js` is the JavaScript entry point.
10099

101100
You can delete or rename the other files.
102101

103102
You may create subdirectories inside `src`. For faster rebuilds, only files inside `src` are processed by Webpack.
104103
You need to **put any JS and CSS files inside `src`**, or Webpack won’t see them.
105104

105+
Only files inside `public` can be used from `public/index.html`.
106+
Read instructions below for using assets from JavaScript and HTML.
107+
106108
You can, however, create more top-level directories.
107109
They will not be included in the production build so you can use them for things like documentation.
108110

@@ -320,7 +322,7 @@ function Header() {
320322
export default function Header;
321323
```
322324

323-
This is currently required for local images. This ensures that when the project is built, webpack will correctly move the images into the build folder, and provide us with correct paths.
325+
This ensures that when the project is built, webpack will correctly move the images into the build folder, and provide us with correct paths.
324326

325327
This works in CSS too:
326328

@@ -334,7 +336,51 @@ Webpack finds all relative module references in CSS (they start with `./`) and r
334336

335337
Please be advised that this is also a custom feature of Webpack.
336338

337-
**It is not required for React** but many people enjoy it (and React Native uses a similar mechanism for images). However it may not be portable to some other environments, such as Node.js and Browserify. If you prefer to reference static assets in a more traditional way outside the module system, please let us know [in this issue](https://github.com/facebookincubator/create-react-app/issues/28), and we will consider support for this.
339+
**It is not required for React** but many people enjoy it (and React Native uses a similar mechanism for images).
340+
An alternative way of handling static assets is described in the next section.
341+
342+
## Using the `public` Folder
343+
344+
>Note: this feature is available with `[email protected]` and higher.
345+
346+
Normally we encourage you to `import` assets in JavaScript files as described above. This mechanism provides a number of benefits:
347+
348+
* Scripts and stylesheets get minified and bundled together to avoid extra network requests.
349+
* Missing files cause compilation errors instead of 404 errors for your users.
350+
* Result filenames include content hashes so you don’t need to worry about browsers caching their old versions.
351+
352+
However there is an **escape hatch** that you can use to add an asset outside of the module system.
353+
354+
If you put a file into the `public` folder, it will **not** be processed by Webpack. Instead it will be copied into the build folder untouched. To reference assets in the `public` folder, you need to use a special variable called `PUBLIC_URL`.
355+
356+
Inside `index.html`, you can use it like this:
357+
358+
```html
359+
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
360+
```
361+
362+
Only files inside the `public` folder will be accessible by `%PUBLIC_URL%` prefix. If you need to use a file from `src` or `node_modules`, you’ll have to copy it there to explicitly specify your intention to make this file a part of the build.
363+
364+
When you run `npm run build`, Create React App will substitute `%PUBLIC_URL%` with a correct absolute path so your project works even if you use client-side routing or host it at a non-root URL.
365+
366+
In JavaScript code, you can use `process.env.PUBLIC_URL` for similar purposes:
367+
368+
```js
369+
render() {
370+
// Note: this is an escape hatch and should be used sparingly!
371+
// Normally we recommend using `import` for getting asset URLs
372+
// as described in “Adding Images and Fonts” above this section.
373+
return <img src={process.env.PUBLIC_URL + '/img/logo.png'} />;
374+
}
375+
```
376+
377+
Keep in mind the downsides of this approach:
378+
379+
* None of the files in `public` folder get post-processed or minified.
380+
* Missing files will not be called at compilation time, and will cause 404 errors for your users.
381+
* Result filenames won’t include content hashes so you’ll need to add query arguments or rename them every time they change.
382+
383+
However, it can be handy for referencing assets like [`manifest.webmanifest`](https://developer.mozilla.org/en-US/docs/Web/Manifest) from HTML, or including small scripts like [`pace.js`](http://github.hubspot.com/pace/docs/welcome/) outside of the bundled code.
338384

339385
## Adding Bootstrap
340386

@@ -558,69 +604,19 @@ HTTPS=true npm start
558604
559605
Note that the server will use a self-signed certificate, so your web browser will almost definitely display a warning upon accessing the page.
560606
561-
## Adding `<link>` and `<meta>` Tags
562-
563-
You can edit the generated `index.html` and add any tags you’d like to it.
564-
565-
### Referring to Static Assets from `<link href>`
566-
567-
>Note: this feature is available with `[email protected]` and higher.
568-
569-
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).
570-
571-
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`:
572-
573-
```html
574-
<link rel="shortcut icon" href="./src/favicon.ico">
575-
<link rel="icon" href="./src/favicon/favicon-16.png" sizes="16x16" type="image/png">
576-
<link rel="icon" href="./src/favicon/favicon-32.png" sizes="32x32" type="image/png">
577-
<link rel="icon" href="./src/favicon/favicon-64.png" sizes="64x64" type="image/png">
578-
```
579-
580-
Webpack will parse those `<link href>` attributes and replace them with real paths.
581-
In production, they will become:
582-
583-
```html
584-
<link rel="shortcut icon" href="/favicon.ico?fd73a6eb">
585-
<link rel="icon" href="/static/media/favicon-16.06a6e0a8.png" sizes="16x16" type="image/png">
586-
<link rel="icon" href="/static/media/favicon-32.eb28da34.png" sizes="32x32" type="image/png">
587-
<link rel="icon" href="/static/media/favicon-64.91cb3479.png" sizes="64x64" type="image/png">
588-
```
589-
590-
For this to work, **make sure to specify paths relatively** so don’t forget the `./`:
591-
592-
```html
593-
<!-- Will be resolved by Webpack on build to the real file. -->
594-
<!-- Use this in most cases: -->
595-
<link rel="icon" href="./src/favicon/favicon-32.png" sizes="32x32" type="image/png">
596-
<!-- See the ./ here: ^^^ -->
597-
598-
<!-- Will actually request http://yourserver.com/src/favicon/favicon-32.png. -->
599-
<!-- Only use this if you know this file will appear on your server and is *not* part of your build: -->
600-
<link rel="icon" href="/src/favicon/favicon-32.png" sizes="32x32" type="image/png">
601-
```
602-
603-
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.
604-
605-
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).
606-
607-
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.
608-
609-
If you need to use an asset from code rather than from HTML, please read [Adding Images and Fonts](#adding-images-and-fonts). For example, to integrate a library like [`react-mdl`](https://github.com/tleunen/react-mdl) that depends on global scripts and styles, [`import` them from JavaScript](https://github.com/tleunen/react-mdl/pull/388).
610-
611-
### Generating Dynamic `<meta>` Tags on the Server
607+
## Generating Dynamic `<meta>` Tags on the Server
612608
613609
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:
614610
615611
```html
616612
<!doctype html>
617613
<html lang="en">
618614
<head>
619-
<meta property="og:title" content="$OG_TITLE">
620-
<meta property="og:description" content="$OG_DESCRIPTION">
615+
<meta property="og:title" content="%OG_TITLE%">
616+
<meta property="og:description" content="%OG_DESCRIPTION%">
621617
```
622618
623-
Then, on the server, regardless of the backend you use, you can read `index.html` into memory and replace `$OG_TITLE`, `$OG_DESCRIPTION`, and any other placeholders with values depending on the current URL. Just make sure to sanitize and escape the interpolated values so that they are safe to embed into HTML!
619+
Then, on the server, regardless of the backend you use, you can read `index.html` into memory and replace `%OG_TITLE%`, `%OG_DESCRIPTION%`, and any other placeholders with values depending on the current URL. Just make sure to sanitize and escape the interpolated values so that they are safe to embed into HTML!
624620
625621
If you use a Node server, you can even share the route matching logic between the client and the server. However duplicating it also works fine in simple cases.
626622

0 commit comments

Comments
 (0)