feat: #3615769 Make the widget JS work without Drupal and serve it as widget/v1.js and v1.css
Stacked on !33 (merged), !35 (merged), !36 (merged) and !37 (merged); review only the last commit(s) until those merge.
Closes #3615769 (closed).
What changes
Runtime without Drupal. The element files (js/ai_answers.{element,markdown,question,answer,sources}.js) no longer touch Drupal, drupalSettings, once or ai/ai_global. Each file is ((scope) => …)(this): on a Drupal page that is window, in the bundle a private object.
Thin Drupal adapter. js/ai_answers.drupal.js, in the element library, provides the CSRF token, Drupal.attachBehaviors for rendered references, and the UI strings as literal Drupal.t() calls, so locale still finds them. It keeps Drupal.aiAnswers as an alias. The blocks behave as before.
Embed mode. The loader sets data-token-url on the answer, and the answer then follows the client contract in docs/placements.md: credentials: 'omit', X-AI-Answers-Token, placement in the body and never agent. The token is kept in memory, renewed under 1 min before expiry, and a 403 gets 1 new token and 1 retry.
Errors. 429 shows the server message, puts the question back in the field, keeps conversation_id and sends nothing before Retry-After. 403, 404 and offline get their own messages, and 403 no longer shows "Could not reach the answer service". A 429 from the token endpoint is waited out and retried once if Retry-After is 30 s or less; a longer one is shown.
Declarative shadow roots. The markup is parsed with Document.parseHTMLUnsafe() and the parsed root's content moved into attachShadow(). Without it, the <template shadowrootmode> is inserted and attachRoot() unwraps it.
Loader (js/ai_answers.embed.js, bundle only). Finds [data-ai-answers="UUID"], fetches each placement once per page and fills the empty elements. Empty elements ask for markup with an ai-answers:markup event when they connect, so late snippets, SPA re-adds and a second copy of the script all work without extra calls. The container gets data-ai-answers-state (loading/ready/error) and ai-answers:ready/ai-answers:error events.
Assets. GET /ai-answers/widget/v1.js and v1.css (WidgetAssetController) concatenate the module's files with no build step. Vendor UMD wrappers export into the bundle's scope, not window or AMD define, and source map references are stripped. Headers: Cache-Control: public, max-age=86400, ETag = content hash (304 on match), Access-Control-Allow-Origin: *. The content is cached until the next cache rebuild.
Feedback icons are inline SVG, so no image request a CSP could block. ai/ai_global is dropped from answer and shadow.answer.
Snippet
<link rel="stylesheet" href="https://example.com/ai-answers/widget/v1.css">
<div data-ai-answers="PLACEMENT-UUID">
<ai-answers-question></ai-answers-question>
<ai-answers-answer></ai-answers-answer>
<ai-answers-sources></ai-answers-sources>
</div>
<script src="https://example.com/ai-answers/widget/v1.js" defer></script>Decisions to look at
- ETag on a content hash, not the module version: dev builds share 1 version string while their content changes.
max-ageis 1 day, not longer: the URL has no version, so it caps how long an update takes to reach visitors.- Inline SVG instead of CSS-inlined icons: no
img-srcneeded.CustomElementsTestnow checks the SVG instead ofthumbs-up.svg. - Embeds don't call
Drupal.attachBehaviorseven when the host page has Drupal: that page is not the site that rendered the reference. - Token 429 auto-retry only for waits of 30 s or less, so a question isn't sent minutes after it was typed.
<ai-answers-follow-up>from the meta snippet isn't supported: the payload has no such element, and follow-ups stay inside the answer.- The
rootIdparam inbuildReferenceItem()is renamed: it was the one ESLint error already on the base commit.
Tests
- New
WidgetAssetTest(Kernel): headers, 304, cache and ETag, bundle order and wrapping, GET only. - New
WidgetRuntimeSourceTest(Unit): no Drupal global, eval orwindow.*leak in the runtime files; the adapter isn't in the bundle. PlacementWidgetEndpointTest: the strings in the runtimet()calls, the adapterDrupal.t()calls and the payload must match.- New
EmbedWidgetTest(FJS) on a bare page fromai_answers_embed_test, with a strict CSP ('self'only, nothing inline): the real token and question endpoints, the template fallback, double load, SPA re-add and late snippet, 429 and 403, 2 placements, iframe. - The FJS tests have not run locally (no WebDriver here); CI is their first run. The same scenarios passed in Chromium through a throwaway Playwright harness, cross-origin under a strict CSP, on both parser paths, and on a Drupal page through the adapter.
AI disclosure
Used AI to help with research, writing and code (splitting the widget JS, writing the asset controller and tests, drafting this description). The reasoning and positions are my own, and I reviewed everything before posting. (Drupal.org AI policy)