Skip to content

Commit ffda543

Browse files
authored
Merge pull request #46 from zensical/extra_js
add description of module, async, defer attributes
2 parents d1cca6a + 8c51dd8 commit ffda543

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

docs/customization.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,57 @@ the `docs` directory:
119119
[instant navigation]: setup/navigation.md#instant-navigation
120120
[RxJS Observable]: https://rxjs.dev/api/index/class/Observable
121121

122+
#### Modules, `async`, `defer`
123+
124+
Is you want to import code as a [JavaScript module], you can simply make sure
125+
that the file has the `.mjs` extension or you can explicitly specify that it is
126+
to be loaded as a module:
127+
128+
[JavaScript module]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
129+
130+
=== "`zensical.toml`"
131+
``` toml
132+
[[project.extra_javascript]]
133+
path = "javascripts/extra.js"
134+
type = "module"
135+
```
136+
137+
=== "`mkdocs.yml`"
138+
``` yaml
139+
extra_javascript:
140+
- path: javascripts/extra.js
141+
type: module
142+
```
143+
144+
This configuration will result in a `type="module"` attribute being added to the
145+
`<script>` tag for the extra JavaScript.
146+
147+
Likewise, you can add [`defer`][defer] and [`async`][async] attributes to the
148+
script tag to further influence how the JavaScript is loaded. For example, for
149+
the `async` case:
150+
151+
=== "`zensical.toml`"
152+
``` toml
153+
[[project.extra_javascript]]
154+
path = "javascripts/extra.js"
155+
async = true
156+
```
157+
158+
=== "`mkdocs.yml`"
159+
``` yaml
160+
extra_javascript:
161+
- path: javascripts/extra.js
162+
async: true
163+
```
164+
165+
[defer]: https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/script#defer
166+
[async]: https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/script#async
167+
168+
Note that Zensical will auto-detect modules by looking at the file extension
169+
only when the `extra_javascript` element is plain text. That means that if you
170+
want to load a module using `async`, you also need to specify the `type`
171+
attribute.
172+
122173
## Extending the theme
123174

124175
!!! info "Template caching"

0 commit comments

Comments
 (0)