Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
refreshless
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
refreshless
Commits
7231a371
Commit
7231a371
authored
1 year ago
by
ambient.impact
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3393105
: Added JavaScript alter hook to move to <head>.
parent
d14779ef
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
modules/refreshless_turbo/src/Hooks/Javascript.php
+50
-0
50 additions, 0 deletions
modules/refreshless_turbo/src/Hooks/Javascript.php
with
50 additions
and
0 deletions
modules/refreshless_turbo/src/Hooks/Javascript.php
0 → 100644
+
50
−
0
View file @
7231a371
<?php
declare
(
strict_types
=
1
);
namespace
Drupal\refreshless_turbo\Hooks
;
use
Drupal\Core\Asset\AttachedAssetsInterface
;
use
Drupal\Core\Language\LanguageInterface
;
use
Drupal\hux\Attribute\Alter
;
/**
* JavaScript hook implementations.
*/
class
Javascript
{
#[Alter('js')]
/**
* Move all JavaScript to the <head> as recommended for Hotwire Turbo.
*
* JavaScript found in the <body> will be executed each time that page is
* loaded whereas JavaScript linked in the <head> will only be executed on a
* full page load. Since all Drupal JavaScript should be using behaviours,
* they should all correctly re-attach on every Turbo visit.
*
* @see \hook_js_alter()
*/
public
function
alter
(
array
&
$javascript
,
AttachedAssetsInterface
$assets
,
LanguageInterface
$language
,
):
void
{
foreach
(
$javascript
as
$filePath
=>
&
$settings
)
{
// Skip items that are already set to the header as they may need to be
// render blocking rather than deferred.
if
(
$settings
[
'scope'
]
===
'header'
)
{
continue
;
}
$settings
[
'scope'
]
=
'header'
;
if
(
!
isset
(
$settings
[
'attributes'
][
'defer'
]))
{
$settings
[
'attributes'
][
'defer'
]
=
true
;
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment