We generally need to reference static resources in a template when developing projects.
When using the command thinkjs
to create a project, it will automatically create the directory www/static
, where specially stores JS, CSS, images and other static resources.
After putting the static resources in www/static
, you can reference them in a template by the following approaches.
<script src="/static/js/foo.js"></script>
<link href="/static/css/foo.css" rel="stylesheet" />
<img src="/static/img/foo.png" alt="" >
Judging whether the request is a static resource request, we use regular expression. The default configuration is as follows.
export default {
resource_on: true, // enable static resources resolution function
resource_reg: /^(static\/|[^\/]+\.(?!js|html)\w+$)/, // regular expression for judging static resource request
}
You can modify the configuration file src/common/config/config.js
according to your project requirements.
After the project is online, it generally uses nginx or other WEB server as a angent. At this time, you can let nginx to directly handle the static resource requests. Thus, you could close the static resources access to improve performance.
Set the option resource_on
to false
in the configuration file src/common/config/env/prodution.js
to close it. eg.
export default {
resource_on: false
}