Add defer attribute to script tags in existing skins #407

Closed
opened 2024-03-24 09:18:41 +00:00 by p3k · 1 comment
p3k commented 2024-03-24 09:18:41 +00:00 (Migrated from github.com)

Alpine.js needs the defer attribute for correct initialization:

Don't forget the "defer" attribute in the <script> tag.

The following shell script adds the attribute to any <script> tag loading the main.js URL:

#!/usr/bin/env sh

dir='/share/www/antville.org/static/sites'

result=$(
  find "$dir" -type f -name '*.skin' -print0 \
    | xargs -0 egrep --ignore-case '<script.+main\.js' \
    | grep --ignore-case --invert-match defer \
    #| head -1 \ # Uncomment for debugging
)

echo "$result" | while IFS=\n read -r line; do
  file=$(echo "$line" | cut -d':' -f1)
  source=$(echo "$line" | cut -d':' -f2)
  target=$(echo "$source" | sed 's|\(<script\)|\1 defer|')

  printf '\nFile: %s\n' "$file"
  printf 'Source: %s\n' "$source"
  printf 'Target: %s\n' "$target"

  # Uncomment to arm the script
  #sed --in-place "s|$source|$target|" "$file"
done
[Alpine.js needs the defer attribute](https://alpinejs.dev/essentials/installation#from-a-script-tag) for correct initialization: > Don't forget the "defer" attribute in the `<script>` tag. The following shell script adds the attribute to any `<script>` tag loading the `main.js` URL: ```shell #!/usr/bin/env sh dir='/share/www/antville.org/static/sites' result=$( find "$dir" -type f -name '*.skin' -print0 \ | xargs -0 egrep --ignore-case '<script.+main\.js' \ | grep --ignore-case --invert-match defer \ #| head -1 \ # Uncomment for debugging ) echo "$result" | while IFS=\n read -r line; do file=$(echo "$line" | cut -d':' -f1) source=$(echo "$line" | cut -d':' -f2) target=$(echo "$source" | sed 's|\(<script\)|\1 defer|') printf '\nFile: %s\n' "$file" printf 'Source: %s\n' "$source" printf 'Target: %s\n' "$target" # Uncomment to arm the script #sed --in-place "s|$source|$target|" "$file" done ```
p3k commented 2024-03-24 10:15:05 +00:00 (Migrated from github.com)

Script ran successfully on Antville.org – closing.

Script ran successfully on Antville.org – closing.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: antville/antville#407
No description provided.