#Use external js in Astro
1 messages · Page 1 of 1 (latest)
Hi @mystic sequoia. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
You can use an external JavaScript file located in the public/ directory of your Astro project in a couple of ways:
- Absolute URL Path: If your JavaScript file is in the
public/directory, you can reference it by an absolute URL path directly in your Astro component templates. For example, you can use a<script>tag with thesrcattribute pointing to the file's location. This approach skips the JavaScript processing, bundling, and optimizations provided by Astro. This method is preferred for any external scripts that have already been published and minified separately from Astro.
<script src="/some-external-script.js"></script>
Source (https://docs.astro.build/en/guides/upgrade-to/v1/)
- Using
is:inlinedirective: To load scripts outside of your project’ssrc/folder, include theis:inlinedirective. This approach also skips the JavaScript processing, bundling, and optimizations that are provided by Astro.
<script is:inline src="/my-script.js"></script>
Source (https://docs.astro.build/en/guides/client-side-scripts/)