跳到正文
DEV Community· Taggor·· 1 小時前AI 評分44

如何在多個網站使用同一個元件,並一次更新所有網站

How to Use the Same Component on Multiple Websites (and Update It Once, Everywhere)

AI 導讀

Taggor 以元件發布方式,讓同一個隔離且有版本管理的元件透過一段程式碼片段載入不同網站,並可透過 Staging 或 Production 頻道切換版本。它使用 Shadow DOM 隔離樣式,以錯誤邊界包裹元件,並讓每次發布建立不可變版本,方便回退。各網站只需加入一個非同步 script tag,無須逐一重新建置和部署。

正文

Copies drift, npm packages make every site rebuild, and a hosted script can break the page. Component publishing keeps one versioned, isolated copy and delivers it to every site with one snippet.

There are four common ways to use one component on several websites: write a native Web Component, share an npm package (or a monorepo), embed an iframe, or load a hosted script.

If every site is built from the same codebase, by the same team, on the same framework, use a package or a monorepo. If the component can live in a fixed box, an iframe is enough. If the sites run on different stacks and the component has to be part of the page, you need a hosted script, which is safe only with three things in place: isolation, versions and a way back. A hosted script with all three is called component publishing, and that is what Taggor does.

What are the four ways, and where does each one fit?

1. A native Web Component, written by hand

How it works. You write a custom element with the browser's own APIs, put it in a .js file and load that file on each site.

Where it fits. A small element with no dependencies, on sites where you control the hosting.

Where it hurts. A Web Component is a format, not a delivery system. Where the file lives, how a new version reaches each site and how you go back are yours to solve. Copy the file into each project, and the original problem is back.

2. A shared npm package or a monorepo

How it works. The component lives in a package. Each site installs it, imports it and builds it into its own bundle.

Where it fits. One framework, one team, sites that have a build step. You get types, tests and code review. In this case it is the right answer.

Where it hurts. You publish the package, then every site bumps the version, rebuilds and deploys. Five sites are five deploys, and a site that skips one drifts. A WordPress theme has no npm install at all.

3. An iframe

How it works. You host the component as a small page of its own, and each site embeds that page with an <iframe> tag.

Where it fits. A self-contained widget that can live in a fixed box: a map, a video player, a booking calendar. Styles do not cross the frame in either direction, and one change to the hosted page reaches every site.

Where it hurts. The widget is a separate document. It does not grow with its content unless you write messaging code between the frame and the page, and it does not inherit the page's fonts and colors. An update overwrites the hosted page, with no versions and no way back unless you build them.

4. A hosted script (a remote embed)

How it works. The component is served from one address. Each site carries a script tag and the component's tag, and the component renders inside the page itself.

Where it fits. Sites on different stacks, or sites you cannot rebuild, when the component has to look and behave like part of the page. A single change reaches every site without a deploy on each.

Where it hurts. Reaching every site at once is also the risk, and the section after the diagram explains why. It is also the approach Taggor makes safe.

Left: three websites, each with its own copy of the same component, each on a different version. Right: one published component reaching the same three websites through one snippet per site, all on the same version.

Why can a hosted script break the page?

Styles collide. The page's CSS reaches into your component, and your component's CSS leaks out into the page.

One error spreads. A script that throws can take other things on the page down with it, and a bad update does that on every site at once.

There is no way back. Say you overwrite widget.js. The old file is gone, and "undo" means fixing forward under pressure.

What is component publishing?

Component publishing is building a piece of a website as its own isolated, versioned unit and delivering it to any site with one snippet.

Here is how Taggor answers the three problems:

  • Isolation. Each component is mounted in a Shadow DOM inside the page, so styles stay on their own side, and an error boundary wraps it. What you write inside the component's tag is fallback content: the visitor sees it until the component loads, or permanently if it cannot load.
  • Versions. Every publish creates a version, and a version's code is immutable.
  • A way back. Sites do not point at a version. They follow a channel, Staging or Production, and a channel points at a version. Going back is moving the pointer.

The snippet is one async script tag per site. It loads a small loader that fetches only the components that site uses. The loader sets no cookies and uses no browser storage.

Taggor also covers the work around the script:

  • Checks before anything goes out. Every publish passes gates for types, size and security, and a file that looks like a secret stops the upload.
  • Your source is kept. Every publish stores the component's source, and npx taggor pull brings it back on any computer.
  • Forms have somewhere to go. A component can declare a collection, and Taggor stores what visitors submit, with anti-spam and export. The data is yours.
  • Your agent can write it. The docs have quickstarts for Claude Code, Codex and Cursor that publish a first component from one prompt.

What does it look like in practice?

The example is a signup form, signup-inline. The finished version in the picture comes from Taggor's own showcase: an email field and a button in one pill, storing each signup in a subscribers collection. The steps below use the starter that init generates, so you can follow them exactly on a new account and put your own form in later.

The signup-inline component from the Taggor showcase, an email field with a Subscribe button, beside example terminal output that publishes it, promotes it to production and rolls it back

Sign in, create the project, preview the component and run the checks:

npx taggor login
npx taggor init --component signup-inline
cd <the folder init printed>
npm install
npx taggor dev
npx taggor check

init creates the project in ~/.taggor/projects/<workspace>/ and prints the path. The starter is one defineTaggorComponent call in components/signup-inline/src/index.tsx, with a single prop, headline. That file is where your form goes. dev serves it on a deliberately hostile test page with aggressive CSS, so collisions show up on your machine, not on a client's site. check runs the publication gates.

Create a test site and publish to it:

npx taggor sites create --name "Test site" --domain <test-hostname> --environment staging
npx taggor publish --site <site key>

sites create prints the site key and the snippet, and npx taggor sites snippet <site key> prints the snippet again whenever you need it. publish builds the component remotely, creates version 1.0.0, moves the Staging channel to it and attaches the component to the site. Paste the snippet once into the page, then write the component's tag where the form should appear:

<script async src="https://cdn.taggorcdn.com/s/<site-key>.js"></script>

<signup-inline headline="Join the list">
  <a href="mailto:hello@example.com">Write to us to join the list</a>
</signup-inline>

The link inside the component's tag is the fallback: with the CDN blocked, it is what stays on the page. The showcase form takes label in place of headline.

A client's live site is a Production site. Create it, attach the component, and paste that site's own snippet and the same component tag into its page. Then move Production to the version you just checked:

npx taggor sites create --name "Client A" --domain <client-a-hostname> --environment production
npx taggor attach signup-inline --site <site key>
npx taggor promote signup-inline --to production

Every further client is the first two commands again: per the docs, all Production sites show the version the Production channel points to. How many sites a workspace can have depends on its plan. On Free, sites create is refused above the plan's site limit; the pricing page has the numbers. The docs have platform guides for WordPress, Shopify, Webflow, Wix, Framer, Squarespace and more.

Update once, change everywhere

Now change the component and publish again:

npx taggor publish
npx taggor promote signup-inline --to production

publish creates a new version (1.0.1 in our run) and moves the Staging channel, so the test site shows it first while Production stays on 1.0.0. promote moves the Production channel, and every Production site follows. No page was edited and no site was rebuilt. The CLI says sites pick up the change within about 60 seconds; the docs allow up to about two minutes, because the loader is cached for a short time.

If the new version is wrong:

npx taggor rollback signup-inline --channel production
npx taggor versions signup-inline

rollback points the Production channel back to 1.0.0, on every Production site at once. versions lists each version and the channels that point to it.

When should you not use this?

  • One codebase, one framework, one team. A package or a monorepo does the job inside the tools you already have.
  • A fixed box is enough. For a widget that does not have to blend into the page, an iframe is simpler.
  • You need a different version on each Production site. In Taggor, two Production sites show the same version of a component. To show different versions, publish the component under another name.
  • You want to keep your existing framework components as they are. A Taggor component is written with the Taggor SDK and renders a Preact view, so existing ones have to be ported.

The short version

Copies drift, a package costs one deploy per site, and an iframe keeps the component in a box. A hosted script changes every site at once, so it needs isolation, versions and a way back.

Taggor gives you those three, plus the checks, the hosting and the form data, behind one snippet per site. If your sites are on different stacks, Taggor takes about ten minutes to try: the CLI quickstart goes from signing in to a component on a page of your own. Start at taggor.dev.

來源:DEV Community · dev.to