3. Paths and assets

For developersVersion 1.0.0Last updated:

  • Relative paths (assets/img.jpg, ../assets/img.jpg, ./assets/img.jpg) are resolved against the section file and, if it doesn’t exist there, against the package root. They are fixed in src, srcset, poster, href (files, not pages), style="...url()" and in <style> blocks, also inside <template>. The same goes for the most common lazy-loading attributes: srcset, data-src, data-srcset, data-bg, data-background, data-poster and <video poster>. Only the value that resolves to an existing package file is rewritten; external URLs, absolute paths and nonexistent files are left as they are. Attributes with custom names (for example data-near-src) aren’t touched: the JS that reads them would receive the relative path and get a 404. The same applies to the value of richtext fields (including the one translated in i18n/<language>.json): when rendering, their relative src, srcset, data-src, data-srcset, poster and data-poster that resolve to a package file are changed to the package URL, so an <img src="img/x.svg"> inside a richtext works without data-src or JS. Only what exists in the package is rewritten, and the result goes in the section cache.
  • The url() entries in the CSS work unchanged because they are served from their folder. If they point to a nonexistent file, the validator warns you.
  • Load order: if index.html exists, the order of its <head> is respected, including external CSS and JS (Google Fonts, GSAP from a CDN…), preconnect tags, inline <style> and inline <script>. Then the rest of the files in css/ and js/ are loaded in natural alphabetical order. To control the order completely, use styles and scripts in bos.json.
    • Inline <script>: by default they are merged into .bos/inline.js and loaded deferred, wrapped in a function (they don’t run before the first paint). If you need one to run earlier (e.g. putting a class on <html> to avoid a flicker), mark it with data-bos-nodelay: BuilderOS prints it inline, not deferred and not wrapped where you put it — in the <head> of index.html it comes out in the page’s <head> (before the package stylesheets); in the <body>, at the end of the page — and “Delay JavaScript” (Performance) doesn’t delay it either. It must be small and not contain </script>; if it does, it stays in the deferred file.
    • <link rel="preload"> in the <head> of index.html (as="font", image, style or script; package path or URL): BuilderOS prints it in the <head> of the pages that load the package, always (it doesn’t depend on any Performance option). Fonts come out with type="font/woff2" (according to the extension) and crossorigin, needed so the browser doesn’t request them twice. If “Preload the package’s woff2 fonts” picks the same URL, it isn’t repeated. A path that doesn’t exist in the package gives a warning on validation.
  • Conditional loading: by default, the CSS and JS are only loaded on pages that use some section of the package. It can be changed in Settings or with "loading": "global" in bos.json.
  • In the editor: the package’s CSS is applied inside the canvas in a scoped way, so a body {} or a reset doesn’t break the WordPress interface. The package’s JS is not loaded in the editor unless you ask for it with "editor": { "scripts": true }.

JS that works in the editor and on the frontend

Initialize per section with BuilderOS.ready. The editor calls it again every time it repaints a block, so your code must tolerate running more than once:

BuilderOS.ready(function (block) {
  // block = elemento raíz de la sección (.bos-block)
  block.querySelectorAll('.slider').forEach(initSlider);
});

For interactive pieces with options the client can change (accordion, tabs, carousel, counter, before/after) use the components (data-bos-component) instead of coding them, or register your own with BuilderOS.component('nombre', function (el, opciones) { …; return { destroy() {} }; }): see the “Interactive components” chapter.