@igor.dvlpr/astro-saferesource
v1.0.1
Published
๐ Adds CSP-compliant inline scripts and styles to Astro! ๐
Downloads
68
Maintainers
Readme
๐ Table of contents
๐ต๐ผ Usage
Install it by executing:
npm i -D "@igor.dvlpr/astro-saferesource"
In order to use it, all you need to do is wrap your existing inline script
or style
tags with the <SafeResource>
component, i.e.:
before.astro
<script is:inline>
console.log('Hello World')
</script>
after.astro
---
import SafeResource from '@igor.dvlpr/astro-saferesource'
---
<SafeResource>
<script is:inline> // <-- do not forget to add is:inline, if not present
console.log('Hello World')
</script>
</SafeResource>
and the component will generate the following output:
after.html
<!-- other page markup -->
<script integrity="sha256-xhIA8fkWcujZdN5EjKW355zTO9eHOZu4D+SzAE4Qqik=">
console.log('Hello World')
</script>
<!-- other page markup -->
[!NOTE] The component outputs a SHA-256 hash.
[!CAUTION] Do NOT forget to add
is:inline
to the element or else Astro will remove it from the<SafeResource>
component in order to optimize it.
[!TIP] The component will log the page, type of resource (script/style) and the generated hashes in the console of the IDE.
โจ Examples
example.astro
---
import SafeResource from '@igor.dvlpr/astro-saferesource'
import Layout from '../layouts/Layout.astro'
---
<Layout title="Welcome to Astro">
<main style="color: wheat;">
<SafeResource>
<script is:inline>
document.addEventListener('DOMContentLoaded', () => {
const easyNav = document.getElementById('easy-nav')
const easyShow = easyNav.getAttribute('data-show')
if (
easyShow === 'whenNeeded' &&
document.documentElement.scrollHeight > window.innerHeight
) {
easyNav.className = 'show'
}
})
</script>
</SafeResource>
<SafeResource>
<style is:inline>
.eren-jaeger {
color: red;
}
</style>
</SafeResource>
</main>
</Layout>
๐ Changelog
๐ The changelog is available here: CHANGELOG.md.
๐ชช License
Licensed under the MIT license which is available here, MIT license.
๐งฌ Related
@igor.dvlpr/astro-post-excerpt
โญ An Astro component that renders post excerpts for your Astro blog - directly from your Markdown and MDX files. Astro v2+ collections are supported as well! ๐
@igor.dvlpr/astro-easynav-button
๐งญ Add an easy-to-use navigational button (jump to top/bottom) to your Astro site. ๐ผ
@igor.dvlpr/magic-queryselector
๐ช A TypeScript-types patch for querySelector/querySelectorAll, make them return types you expect them to! ๐ฎ
๐ง Zep is a zero-dependency, efficient debounce module. โฐ
๐ก Parse, manage, compare and output SemVer-compatible version numbers. ๐ก
๐จ๐ปโ๐ป Author
Created by Igor Dimitrijeviฤ (@igorskyflyer).