Subscribe with Google


Preferred Sources


Newsletter


Survey


Rewarded Ads


Custom CTA


Reader Registration


Using Extended Access


Syncing Publisher Entitlements to Google


Receiving Entitlements from Google


Cancelling Entitlements Plans or Making Refunds


Content examples

Preferred Sources: Implement with ESM

This article demonstrates an alternative method for integrating the Preferred Sources library using an ES Module (.mjs) distribution. Alongside standard script tag inclusion, developers working in environments that utilize module bundlers or <script type="module"> tags can opt to import the SDK directly and coordinate runtime execution using top-level imports.

Try it out

Below is a live widget instantiated entirely via top-level ES Module import and direct instance interaction:


How to Implement

  1. Import preferredSource directly from publisher.mjs.
  2. Call preferredSource.init() directly on the imported instance to configure runtime options.
  3. Bind flow initiation directly to custom UI triggers using .onclick = () => { preferredSource.addPreferredSource(); }.
<script type="module">
  // 1. Import module singleton directly
  import { preferredSource } from "https://news.google.com/swg/js/v1/publisher.mjs";

  // 2. Initialize directly using the imported module instance
  preferredSource.init({ theme: 'dark' });

  // 3. Programmatically bind flow invocation via onclick
  const triggerBtn = document.querySelector('#custom-esm-trigger-btn');
  triggerBtn.onclick = () => {
    preferredSource.addPreferredSource();
  };
</script>
Fork me on GitHub