CodePen external scripts execution order between editor preview and published embed
0 reputation · 21 Jul 2024, 18:52 UTC
0 reputation · 21 Jul 2024, 18:52 UTC
CodePen Pens support adding external CSS and JS via Pen Settings > Add External Stylesheets/Scripts. The URLs are injected into a sandboxed preview iframe.
The editor preview uses a development iframe with permissive loading, while published Pen pages and embeds apply stricter Content Security Policy and async handling that can change when resources become available. CodePen does not document a guaranteed synchronous execution order for multiple external scripts added through the UI, and order is best-effort and can differ between preview and production render.
Relative paths and localhost references resolve in local files but are blocked in CodePen's sandboxed iframe, requiring absolute HTTPS URLs for assets to load in production. Preprocessor selection is server-side on CodePen and output can diverge from a local toolchain.
What is the documented execution guarantee for multiple external scripts added via Pen Settings in a published embed versus the editor preview? Does CodePen apply specific loading attributes or CSP directives to injected script tags in production that affect timing? How should dependency order between external scripts be expressed when no synchronous guarantee is documented?
35275 reputation · 22 Jul 2024, 04:58 UTC
CodePen does not document a guaranteed synchronous execution order for multiple external scripts added via Pen Settings, in either the editor preview or published embeds. Ordering is best-effort and can differ between environments.
<script src="…"></script> elements in the listed order, but browsers load them asynchronously by default. CodePen does not confirm that defer is applied in preview.async or defer attributes, and CSP directives (e.g., script-src) can block inline scripts, eval(), and non-HTTPS resources.The platform treats script injection as a convenience, not a dependency manager. In preview, the browser's native async loading dominates. In production, CSP and possible attribute mutations add further nondeterminism. Because CodePen does not version or document the injection mechanics, a pen that works today may reorder scripts after a platform update.
type="module" and explicit import/export statements; the module graph defines order.await import(url)).# 1. Inspect load order and attributes on the published pen
open https://codepen.io//pen/ in browser → DevTools → Network tab → filter JS
# 2. Check CSP header
curl -I https://codepen.io//pen/ | grep -i content-security-policy
# 3. Minimal test pen: two scripts logging "A loaded" / "B loaded"
# Compare console order in preview vs published embed
# 4. If using ES modules, confirm module graph loads in published embedDoes your pen rely on globals exposed by one script being available to another at parse time? If yes, bundling or ES modules is required; a loader script with await import() also works but adds async latency.
Use comments to ask for clarification. Post a solution as an answer.
35,275 reputation · 21 Jul 2024, 23:34 UTC
In the editor preview the external scripts are injected synchronously as they are added, so they execute in the order they appear. In a published embed the same URLs are wrapped with async or defer attributes and subject to CSP, which can delay or reorder execution, especially when scripts come from different origins. To guarantee ordering, list the scripts in the desired sequence or use dynamic import after the first script loads.