Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions themes/develop/include/head/js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@
{% endif %}

<!-- Vite -->
{# vite() の出力は <script type="module"> なので、仕様上すでに defer 相当(HTMLのパース完了後に実行)。
async を付けるとその defer が打ち消され、モジュールの読み込みが終わった時点で即実行されるため、
<body> の生成前に main.js が走ることがある。そうなると DOM を参照する処理が対象0件で空振りする
(Alpine の初期化警告、externalLinks による target/rel の付与漏れ)。
ダウンロードは modulepreload が先行するので、async を外しても取得は遅くならない。 #}
{% if touch('Touch_NotAdmin') %}
{{ vite('src/js/main.js', { scriptTagAttributes: { async: true } }) }}
{{ vite('src/js/main.js') }}
{% endif %}

{% if touch('Touch_Admin') %}
{{ vite(['src/js/main.js', 'src/js/admin.js'], { scriptTagAttributes: { async: true } }) }}
{{ vite(['src/js/main.js', 'src/js/admin.js']) }}
{% endif %}

<script>
Expand Down
28 changes: 15 additions & 13 deletions themes/develop/src/js/lib/build-in.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,21 @@ const linkMatchLocation = (context) => {
* @param {Document | Element} context
*/
const externalLinks = (context) => {
const selector = 'a:not([target]):not([href^="javascript"]):not([href^="tel"])';
const targets = context.querySelectorAll(selector);
const innerlinkPtn = new RegExp(`${window.location.hostname}(:\\d+)*`);
[].forEach.call(targets, (target) => {
const href = target.getAttribute('href');
if (innerlinkPtn.exec(href)) {
return;
}
if (!/^(https?)?:/.test(href)) {
return;
}
target.setAttribute('target', '_blank');
target.setAttribute('rel', 'noopener noreferrer');
domContentLoaded(() => {
const selector = 'a:not([target]):not([href^="javascript"]):not([href^="tel"])';
const targets = context.querySelectorAll(selector);
const innerlinkPtn = new RegExp(`${window.location.hostname}(:\\d+)*`);
[].forEach.call(targets, (target) => {
const href = target.getAttribute('href');
if (innerlinkPtn.exec(href)) {
return;
}
if (!/^(https?)?:/.test(href)) {
return;
}
target.setAttribute('target', '_blank');
target.setAttribute('rel', 'noopener noreferrer');
});
});
};

Expand Down