mirror of
https://github.com/zhigang1992/create-react-app.git
synced 2026-03-29 22:38:26 +08:00
Move using the public folder to its own file
This commit is contained in:
@@ -11,10 +11,6 @@ You can find the most recent version of this guide [here](https://github.com/fac
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [Using the `public` Folder](#using-the-public-folder)
|
||||
- [Changing the HTML](#changing-the-html)
|
||||
- [Adding Assets Outside of the Module System](#adding-assets-outside-of-the-module-system)
|
||||
- [When to Use the `public` Folder](#when-to-use-the-public-folder)
|
||||
- [Using Global Variables](#using-global-variables)
|
||||
- [Adding Bootstrap](#adding-bootstrap)
|
||||
- [Using a Custom Theme](#using-a-custom-theme)
|
||||
@@ -41,70 +37,6 @@ You can find the most recent version of this guide [here](https://github.com/fac
|
||||
- [Advanced Configuration](#advanced-configuration)
|
||||
- [Alternatives to Ejecting](#alternatives-to-ejecting)
|
||||
|
||||
## Using the `public` Folder
|
||||
|
||||
> Note: this feature is available with `react-scripts@0.5.0` and higher.
|
||||
|
||||
### Changing the HTML
|
||||
|
||||
The `public` folder contains the HTML file so you can tweak it, for example, to [set the page title](#changing-the-page-title).
|
||||
The `<script>` tag with the compiled code will be added to it automatically during the build process.
|
||||
|
||||
### Adding Assets Outside of the Module System
|
||||
|
||||
You can also add other assets to the `public` folder.
|
||||
|
||||
Note that we normally encourage you to `import` assets in JavaScript files instead.
|
||||
For example, see the sections on [adding a stylesheet](#adding-a-stylesheet) and [adding images and fonts](#adding-images-fonts-and-files).
|
||||
This mechanism provides a number of benefits:
|
||||
|
||||
- Scripts and stylesheets get minified and bundled together to avoid extra network requests.
|
||||
- Missing files cause compilation errors instead of 404 errors for your users.
|
||||
- Result filenames include content hashes so you don’t need to worry about browsers caching their old versions.
|
||||
|
||||
However there is an **escape hatch** that you can use to add an asset outside of the module system.
|
||||
|
||||
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`.
|
||||
|
||||
Inside `index.html`, you can use it like this:
|
||||
|
||||
```html
|
||||
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
|
||||
```
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
In JavaScript code, you can use `process.env.PUBLIC_URL` for similar purposes:
|
||||
|
||||
```js
|
||||
render() {
|
||||
// Note: this is an escape hatch and should be used sparingly!
|
||||
// Normally we recommend using `import` for getting asset URLs
|
||||
// as described in “Adding Images and Fonts” above this section.
|
||||
return <img src={process.env.PUBLIC_URL + '/img/logo.png'} />;
|
||||
}
|
||||
```
|
||||
|
||||
Keep in mind the downsides of this approach:
|
||||
|
||||
- None of the files in `public` folder get post-processed or minified.
|
||||
- Missing files will not be called at compilation time, and will cause 404 errors for your users.
|
||||
- Result filenames won’t include content hashes so you’ll need to add query arguments or rename them every time they change.
|
||||
|
||||
### When to Use the `public` Folder
|
||||
|
||||
Normally we recommend importing [stylesheets](#adding-a-stylesheet), [images, and fonts](#adding-images-fonts-and-files) from JavaScript.
|
||||
The `public` folder is useful as a workaround for a number of less common cases:
|
||||
|
||||
- You need a file with a specific name in the build output, such as [`manifest.webmanifest`](https://developer.mozilla.org/en-US/docs/Web/Manifest).
|
||||
- You have thousands of images and need to dynamically reference their paths.
|
||||
- You want to include a small script like [`pace.js`](http://github.hubspot.com/pace/docs/welcome/) outside of the bundled code.
|
||||
- Some library may be incompatible with Webpack and you have no other option but to include it as a `<script>` tag.
|
||||
|
||||
Note that if you add a `<script>` that declares global variables, you also need to read the next section on using them.
|
||||
|
||||
## Using Global Variables
|
||||
|
||||
When you include a script in the HTML file that defines global variables and try to use one of these variables in the code, the linter will complain because it cannot see the definition of the variable.
|
||||
|
||||
66
docusaurus/docs/using-the-public-folder.md
Normal file
66
docusaurus/docs/using-the-public-folder.md
Normal file
@@ -0,0 +1,66 @@
|
||||
---
|
||||
id: using-the-public-folder
|
||||
title: Using the `public` Folder
|
||||
---
|
||||
|
||||
> Note: this feature is available with `react-scripts@0.5.0` and higher.
|
||||
|
||||
### Changing the HTML
|
||||
|
||||
The `public` folder contains the HTML file so you can tweak it, for example, to [set the page title](#changing-the-page-title).
|
||||
The `<script>` tag with the compiled code will be added to it automatically during the build process.
|
||||
|
||||
### Adding Assets Outside of the Module System
|
||||
|
||||
You can also add other assets to the `public` folder.
|
||||
|
||||
Note that we normally encourage you to `import` assets in JavaScript files instead.
|
||||
For example, see the sections on [adding a stylesheet](#adding-a-stylesheet) and [adding images and fonts](#adding-images-fonts-and-files).
|
||||
This mechanism provides a number of benefits:
|
||||
|
||||
- Scripts and stylesheets get minified and bundled together to avoid extra network requests.
|
||||
- Missing files cause compilation errors instead of 404 errors for your users.
|
||||
- Result filenames include content hashes so you don’t need to worry about browsers caching their old versions.
|
||||
|
||||
However there is an **escape hatch** that you can use to add an asset outside of the module system.
|
||||
|
||||
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`.
|
||||
|
||||
Inside `index.html`, you can use it like this:
|
||||
|
||||
```html
|
||||
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
|
||||
```
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
In JavaScript code, you can use `process.env.PUBLIC_URL` for similar purposes:
|
||||
|
||||
```js
|
||||
render() {
|
||||
// Note: this is an escape hatch and should be used sparingly!
|
||||
// Normally we recommend using `import` for getting asset URLs
|
||||
// as described in “Adding Images and Fonts” above this section.
|
||||
return <img src={process.env.PUBLIC_URL + '/img/logo.png'} />;
|
||||
}
|
||||
```
|
||||
|
||||
Keep in mind the downsides of this approach:
|
||||
|
||||
- None of the files in `public` folder get post-processed or minified.
|
||||
- Missing files will not be called at compilation time, and will cause 404 errors for your users.
|
||||
- Result filenames won’t include content hashes so you’ll need to add query arguments or rename them every time they change.
|
||||
|
||||
### When to Use the `public` Folder
|
||||
|
||||
Normally we recommend importing [stylesheets](#adding-a-stylesheet), [images, and fonts](#adding-images-fonts-and-files) from JavaScript.
|
||||
The `public` folder is useful as a workaround for a number of less common cases:
|
||||
|
||||
- You need a file with a specific name in the build output, such as [`manifest.webmanifest`](https://developer.mozilla.org/en-US/docs/Web/Manifest).
|
||||
- You have thousands of images and need to dynamically reference their paths.
|
||||
- You want to include a small script like [`pace.js`](http://github.hubspot.com/pace/docs/welcome/) outside of the bundled code.
|
||||
- Some library may be incompatible with Webpack and you have no other option but to include it as a `<script>` tag.
|
||||
|
||||
Note that if you add a `<script>` that declares global variables, you also need to read the next section on using them.
|
||||
@@ -25,7 +25,8 @@
|
||||
"adding-a-css-modules-stylesheet",
|
||||
"adding-a-sass-stylesheet",
|
||||
"post-processing-css",
|
||||
"adding-images-fonts-and-files"
|
||||
"adding-images-fonts-and-files",
|
||||
"using-the-public-folder"
|
||||
],
|
||||
"Testing": ["running-tests", "debugging-tests"],
|
||||
"Deployment": ["publishing-components-to-npm", "deployment"],
|
||||
|
||||
Reference in New Issue
Block a user