Skip to content

Commit f1c9584

Browse files
authoredSep 2, 2016
Disable implicit serving of the source files (#551)
* Disable contentBase in development * Document wmonk#428
1 parent 98f74a8 commit f1c9584

File tree

2 files changed

+71
-4
lines changed

2 files changed

+71
-4
lines changed
 

‎scripts/start.js

+17
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,23 @@ function addMiddleware(devServer) {
221221

222222
function runDevServer(port) {
223223
var devServer = new WebpackDevServer(compiler, {
224+
// By default WebpackDevServer also serves files from the current directory.
225+
// This might be useful in legacy apps. However we already encourage people
226+
// to use Webpack for importing assets in the code, so we don't need to
227+
// additionally serve files by their filenames. Otherwise, even if it
228+
// works in development, those files will be missing in production, unless
229+
// we explicitly copy them. But even if we copy the all the files into
230+
// the build output (which doesn't seem to be wise because it may contain
231+
// private information such as files with API keys, for example), we would
232+
// still have a problem. Since the filenames would be the same every time,
233+
// browsers would cache their content, and updating file content would not
234+
// work correctly. This is easily solved by importing assets through Webpack
235+
// because if it can then append content hashes to filenames in production,
236+
// just like it does for JS and CSS. And because we configured "html" loader
237+
// to be used for HTML files, even <link href="./src/something.png"> would
238+
// get resolved correctly by Webpack and handled both in development and
239+
// in production without actually serving it by that path.
240+
contentBase: [],
224241
// Enable hot reloading server. It will provide /sockjs-node/ endpoint
225242
// for the WebpackDevServer client so it can learn when the files were
226243
// updated. The WebpackDevServer client is included as an entry point

‎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)
@@ -526,11 +528,59 @@ If the `proxy` option is **not** flexible enough for you, alternatively you can:
526528
* Enable CORS on your server ([here’s how to do it for Express](http://enable-cors.org/server_expressjs.html)).
527529
* Use [environment variables](#adding-custom-environment-variables) to inject the right server host and port into your app.
528530
529-
## Adding `<meta>` Tags
531+
## Adding `<link>` and `<meta>` Tags
530532
531-
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.
533+
You can edit the generated `index.html` and add any tags you’d like to it.
532534
533-
To solve this, we recommend to add placeholders into the HTML, like this:
535+
### Referring to Static Assets from `<link href>`
536+
537+
>Note: this feature is available with `react-scripts@0.3.0` and higher.
538+
539+
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).
540+
541+
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`:
542+
543+
```html
544+
<link rel="shortcut icon" href="./src/favicon.ico">
545+
<link rel="icon" href="./src/favicon/favicon-16.png" sizes="16x16" type="image/png">
546+
<link rel="icon" href="./src/favicon/favicon-32.png" sizes="32x32" type="image/png">
547+
<link rel="icon" href="./src/favicon/favicon-64.png" sizes="64x64" type="image/png">
548+
```
549+
550+
Webpack will parse those `<link href>` attributes and replace them with real paths.
551+
In production, they will become:
552+
553+
```html
554+
<link rel="shortcut icon" href="/favicon.ico?fd73a6eb">
555+
<link rel="icon" href="/static/media/favicon-16.06a6e0a8.png" sizes="16x16" type="image/png">
556+
<link rel="icon" href="/static/media/favicon-32.eb28da34.png" sizes="32x32" type="image/png">
557+
<link rel="icon" href="/static/media/favicon-64.91cb3479.png" sizes="64x64" type="image/png">
558+
```
559+
560+
For this to work, **make sure to specify paths relatively** so don’t forget the `./`:
561+
562+
```html
563+
<!-- Will be resolved by Webpack on build to the real file. -->
564+
<!-- Use this in most cases: -->
565+
<link rel="icon" href="./src/favicon/favicon-32.png" sizes="32x32" type="image/png">
566+
<!-- See the ./ here: ^^^ -->
567+
568+
<!-- Will actually request http://yourserver.com/src/favicon/favicon-32.png. -->
569+
<!-- Only use this if you know this file will appear on your server and is *not* part of your build: -->
570+
<link rel="icon" href="/src/favicon/favicon-32.png" sizes="32x32" type="image/png">
571+
```
572+
573+
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.
574+
575+
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).
576+
577+
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.
578+
579+
If you need to use an asset from code rather than from HTML, please read [Adding Images and Fonts](#adding-images-and-fonts).
580+
581+
### Generating Dynamic `<meta>` Tags on the Server
582+
583+
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:
534584
535585
```html
536586
<!doctype html>

0 commit comments

Comments
 (0)
Please sign in to comment.