0.14.4 • Published 8 months ago

@lwrjs/everywhere v0.14.4

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

LWR Everywhere

LWR Everywhere allows you to embed Lightning web components into your web page or app using just a few lines of code. LWR Everywhere is powered by the Lightning Web Stack, which provides:

Displaying components

This section goes through the steps to display a Lightning web component on your website. No special tools or setup needed!

1. Add a container to the HTML

First, open the HTML page you want to edit. Add an empty <div> tag anywhere within the <body> to mark where you want to display a Lightning web component. For example:

<!-- ... existing HTML ... -->
<div id="embed-stats"></div>
<!-- ... existing HTML ... -->

This <div> has a unique id attribute. This allows us to find it later so we can embed a component in it.

2. Import the LWR Everywhere Module

Add a JavaScript module script to the HTML page. For example:

<script type="module" src="lwre.js"></script>

Import the authenticate and createComponent APIs from the LWR Everywhere module. For example:

// lwre.js
// Import the LWR Everywhere module
import { authenticate, createComponent } from 'https://lwr-server.com/resource/path/lwr-everywhere.js';

Tip: Use a minified version of the LWR Everywhere module by importing lwr-everywhere-min.js and a debug version with lwr-everywhere-debug.js.

3. Authenticate

In order to access components from your Salesforce org, you must be authenticated. LWR Everywhere doesn’t handle authentication, so you must provide it with a Salesforce authentication token and instance URL. For example:

// lwre.js
import { authenticate, createComponent } from 'https://lwr-server.com/resource/path/lwr-everywhere.js';

const { access_token, instance_url } = getAuthData(); // you write this logic
authenticate({ access_token, instance_url });

Note: The authentication data is obtained from an OAuth flow into your Connected App.

4. Display a component

After authenticating, embed a Lightning web component on your website. For example:

// lwre.js
import { authenticate, createComponent } from 'https://lwr-server.com/resource/path/lwr-everywhere.js';

const { access_token, instance_url } = getAuthData();
authenticate({ access_token, instance_url });

createComponent('my/stats', 'embed-stats', { category: 'cats', darkMode: true });

And that's it!

This one line of code displays the "my/stats" component in the <div> we created in the first step, and passes in some public properties (category and darkMode).

Tip: Check out the API reference for authenticate and createComponent.

Interactivity

After creating a component, you can interact with it.

Update component properties

Update a component's public properties to add reactivity. For example:

const container = document.querySelector('#counter-container');
const counter = createComponent('my/counter', container, { count: 6 });
// ... time passes ...
counter.properties = { count: 9 };

Client-side Navigation

If the embedded component uses the LWR Client-side Router, turn on the navigation setting. Then you can navigate programmatically and subscribe to navigation events and errors. For example:

import { createComponent } from 'https://lwr-server.com/resource/path/lwr-everywhere.js';
const cmp = createComponent('my/cmp', 'container', { mode: 'compact' }, { navigation: true });

// Navigate by updating the current page reference
cmp.pageReference = { type: 'standard__namedPage', attributes: { pageName: 'home' } };

// Subscribe to navigation events and errors
cmp.addEventListener('navigate', ({ detail }) => {
    console.log('Navigated to page reference:', detail);
});
cmp.addEventListener('navigationerror', ({ detail: { code, message } }) => {
    console.error(`Navigation error: ${code} - ${message}`);
});

UI API Adapters

Important: This feature is currently under construction!

LWR Everywhere supports the UI API adapters. For example, this code shows an embedded component requesting a list of Opportunities from a Salesforce org via the getListInfoByName wire:

import { LightningElement, wire } from 'lwc';
import { getListInfoByName } from 'lightning/uiListApi';

export default class MyData extends LightningElement {
    opportunities = [];
    @wire(getListInfoByName, {
        objectApiName: 'Opportunity',
        listViewApiName: 'AllOpportunities',
    })
    listView({ error, data }): void {
        if (data) {
            this.opportunities = data.records.records;
        } else if (error) {
            console.error(`Failed to fetch Opportunities: ${error.body.message}`);
        }
    }
}

Connected App setup

To enable authentication with LWR Everywhere, follow these steps to set up a secure Connected App:

  1. Create a Connected App with OAuth Settings using these instructions and:
    1. If a client-side authentication flow is being used, set the "Callback URL" to the URL of the HTML page (e.g. "https://my-website.com/page")
    2. Make a note of the "Consumer Key" and "Consumer Secret" (if applicable)
    3. Under Setup -> Manage Connected Apps, click "Edit" for the app and set "Permitted Users" to "Admin approved users are pre-authorized" and "Save"
    4. Approve Profiles for Connected App access:
      1. Go to Setup -> Profiles
      2. Select the desired Profile and click "Assigned Connected Apps"
      3. Add the Connected App to the "Enabled Connected Apps" list and click "Save"
  2. Enabled CORS for the website, if a client-side authentication flow is being used:
    1. Go to Setup -> CORS
    2. Add a new "Allowed Origin" for the website (e.g. "https://my-website.com")
  3. Setup a Content Security Policy (CSP)
    1. Add a script-src CSP to the web page containing the Connected App origin, and 'unsafe-eval' to support Lightning Web Security. For example:
    <meta
        http-equiv="Content-Security-Policy"
        content="script-src 'unsafe-eval' https://connected-app-server.com"
    />

Build your own LWR Everywhere module

For the majority of use cases, you simply import the LWR Everywhere Module from your Salesforce org. However, you can also generate your own custom LWR Everywhere module and host it yourself. For example:

// Import my custom LWR Everywhere module
import { authenticate, createComponent } from './static/my-lwr-everywhere.js';

Use the generate() API from the @lwrjs/everywhere/generate server module to build the file. For example:

// my-package/scripts/generate-client-runtime.mjs
import { generate } from '@lwrjs/everywhere/generate';

generate({
    format: 'amd', // this is the only required option
    server: 'https://lwr-server.com',
    apiVersion: 'v57.0',
    apiPrefix: '/lwr',
    locale: 'fr',
    bundle: false,
    debug: true,
    minify: true,
    outFile: 'static-site/public/lwrEverywhere.js',
})
    .then(() => {
        console.log('>> successfully built the LWR Everywhere module');
        process.exit(0);
    })
    .catch((e) => {
        console.error('>> ERROR generating the LWR Everywhere module:', e);
        process.exit(1);
    });

Tip: Check out the API reference for generate.

API reference

Client APIs

Import these functions from the LWR Everywhere Module.

authenticate

interface AuthData {
    access_token: string;
    instance_url: string;
}
type AuthenticateFunction = (authData?: AuthData) => void;

createComponent

type CreateComponentFunction = (
    specifier: string, // component specifier: "namespace/name" OR versioned: "namespace/name/v/2.0"
    nodeId: string | HTMLElement, // either a DOM node or its id
    properties: Record<string, any>, // default: {}
    config: { navigation: boolean }, // default { navigation: false }
) => Promise<EverywhereComponent>;

interface EverywhereComponent extends Element {
    properties: Record<string, any>; // update a component's public properties
    pageReference?: PageReference; // update the current page reference in the LWR Router
}

interface PageReference {
    type: string;
    attributes?: Record<string, string | null>;
    state?: Record<string, string | null>;
}

Server APIs

Import these functions from @lwrjs/everywhere/generate.

generate

interface GenerationOptions {
    // Required options
    format: 'esm' | 'amd'; // format for component code, LWR-S only supports 'amd'

    // Optional
    server?: string; // LWR server origin, default: import.meta.url
    apiVersion?: string; // LWR API version (eg: 'v57.0'), default: '1'
    apiPrefix?: string; // LWR API prefix (eg: '/lwr'), default: ''
    locale?: string; // default: LWR server default locale
    bundle?: boolean; // bundle the component code if true, default: true
    debug?: boolean; // use debug mode if true, default: false

    // File options, relative paths appended to the CWD
    minify?: boolean; // minify the module code if true, default: false
    outFile?: string; // the module output file path, default: '__lwr_client__/lwr-everywhere.js'
}

type GenerateFunction = (options: GenerationOptions) => Promise<void>;
0.15.0-alpha.33

9 months ago

0.15.0-alpha.35

9 months ago

0.15.0-alpha.34

9 months ago

0.15.0-alpha.36

8 months ago

0.13.10

9 months ago

0.15.0-alpha.32

9 months ago

0.13.9

9 months ago

0.15.0-alpha.31

9 months ago

0.15.0-alpha.28

9 months ago

0.15.0-alpha.27

9 months ago

0.15.0-alpha.29

9 months ago

0.15.0-alpha.30

9 months ago

0.14.4

9 months ago

0.15.0-alpha.19

9 months ago

0.15.0-alpha.18

9 months ago

0.15.0-alpha.22

9 months ago

0.15.0-alpha.21

9 months ago

0.15.0-alpha.24

9 months ago

0.15.0-alpha.23

9 months ago

0.15.0-alpha.26

9 months ago

0.15.0-alpha.25

9 months ago

0.15.0-alpha.20

9 months ago

0.15.0-alpha.17

10 months ago

0.15.0-alpha.16

10 months ago

0.15.0-alpha.15

10 months ago

0.15.0-alpha.14

10 months ago

0.13.8

10 months ago

0.14.3

10 months ago

0.15.0-alpha.13

10 months ago

0.14.2

10 months ago

0.15.0-alpha.12

10 months ago

0.15.0-alpha.11

11 months ago

0.15.0-alpha.10

11 months ago

0.15.0-alpha.0

11 months ago

0.15.0-alpha.7

11 months ago

0.15.0-alpha.8

11 months ago

0.15.0-alpha.3

11 months ago

0.15.0-alpha.1

11 months ago

0.15.0-alpha.2

11 months ago

0.15.0-alpha.9

11 months ago

0.13.0-alpha.9

1 year ago

0.13.0-alpha.8

1 year ago

0.13.6

11 months ago

0.13.7

11 months ago

0.13.0

12 months ago

0.13.1

12 months ago

0.13.2

12 months ago

0.13.3

12 months ago

0.13.4

11 months ago

0.13.5

11 months ago

0.14.0

11 months ago

0.14.1

11 months ago

0.13.0-alpha.12

1 year ago

0.13.0-alpha.13

1 year ago

0.13.0-alpha.14

1 year ago

0.13.0-alpha.15

1 year ago

0.13.0-alpha.10

1 year ago

0.13.0-alpha.11

1 year ago

0.13.0-alpha.27

1 year ago

0.13.0-alpha.28

1 year ago

0.13.0-alpha.29

1 year ago

0.13.0-alpha.30

12 months ago

0.12.3

1 year ago

0.12.4

1 year ago

0.13.0-alpha.16

1 year ago

0.13.0-alpha.17

1 year ago

0.13.0-alpha.18

1 year ago

0.13.0-alpha.19

1 year ago

0.13.0-alpha.23

1 year ago

0.13.0-alpha.24

1 year ago

0.13.0-alpha.25

1 year ago

0.13.0-alpha.26

1 year ago

0.13.0-alpha.20

1 year ago

0.13.0-alpha.21

1 year ago

0.13.0-alpha.22

1 year ago

0.13.0-alpha.7

1 year ago

0.13.0-alpha.6

1 year ago

0.13.0-alpha.5

1 year ago

0.13.0-alpha.4

1 year ago

0.13.0-alpha.3

1 year ago

0.13.0-alpha.2

1 year ago

0.13.0-alpha.1

1 year ago

0.12.0

1 year ago

0.12.1

1 year ago

0.12.2

1 year ago

0.13.0-alpha.0

1 year ago

0.12.0-alpha.31

1 year ago

0.12.0-alpha.30

1 year ago

0.12.0-alpha.29

1 year ago

0.12.0-alpha.28

1 year ago

0.12.0-alpha.26

1 year ago

0.12.0-alpha.27

1 year ago

0.12.0-alpha.25

1 year ago

0.12.0-alpha.24

1 year ago

0.12.0-alpha.22

1 year ago

0.12.0-alpha.23

1 year ago

0.12.0-alpha.21

1 year ago

0.12.0-alpha.20

1 year ago

0.12.0-alpha.19

1 year ago

0.12.0-alpha.18

1 year ago

0.11.15

1 year ago

0.11.14

1 year ago

0.12.0-alpha.17

1 year ago

0.12.0-alpha.15

1 year ago

0.12.0-alpha.16

1 year ago

0.12.0-alpha.14

1 year ago

0.12.0-alpha.13

1 year ago

0.12.0-alpha.12

1 year ago

0.12.0-alpha.11

1 year ago

0.12.0-alpha.10

1 year ago

0.12.0-alpha.8

1 year ago

0.12.0-alpha.9

1 year ago

0.12.0-alpha.6

1 year ago

0.12.0-alpha.7

1 year ago

0.11.13

1 year ago

0.12.0-alpha.5

1 year ago

0.12.0-alpha.4

1 year ago

0.9.6

2 years ago

0.11.12

2 years ago

0.11.11

2 years ago

0.11.10

2 years ago

0.12.0-alpha.3

2 years ago

0.11.9

2 years ago

0.10.14

2 years ago

0.11.8

2 years ago

0.12.0-alpha.2

2 years ago

0.11.0-alpha.6

2 years ago

0.11.0-alpha.8

2 years ago

0.11.0-alpha.7

2 years ago

0.11.0-alpha.9

2 years ago

0.11.0-alpha.0

2 years ago

0.11.0-alpha.2

2 years ago

0.11.0-alpha.1

2 years ago

0.11.0-alpha.4

2 years ago

0.11.0-alpha.3

2 years ago

0.9.5

2 years ago

0.10.9

2 years ago

0.10.1

2 years ago

0.10.2

2 years ago

0.10.3

2 years ago

0.10.4

2 years ago

0.10.5

2 years ago

0.10.6

2 years ago

0.10.7

2 years ago

0.10.8

2 years ago

0.10.0

2 years ago

0.11.0-alpha.13

2 years ago

0.11.0-alpha.12

2 years ago

0.11.0-alpha.11

2 years ago

0.11.0-alpha.10

2 years ago

0.11.0-alpha.15

2 years ago

0.11.0-alpha.14

2 years ago

0.11.0

2 years ago

0.11.1

2 years ago

0.11.2

2 years ago

0.11.3

2 years ago

0.11.4

2 years ago

0.10.10

2 years ago

0.11.5

2 years ago

0.10.11

2 years ago

0.11.6

2 years ago

0.10.12

2 years ago

0.10.13

2 years ago

0.11.7

2 years ago

0.12.0-alpha.0

2 years ago

0.12.0-alpha.1

2 years ago

0.10.0-alpha.21

2 years ago

0.10.0-alpha.22

2 years ago

0.10.0-alpha.20

2 years ago

0.10.0-alpha.7

2 years ago

0.10.0-alpha.6

2 years ago

0.10.0-alpha.9

2 years ago

0.10.0-alpha.8

2 years ago

0.10.0-alpha.10

2 years ago

0.10.0-alpha.12

2 years ago

0.10.0-alpha.11

2 years ago

0.10.0-alpha.14

2 years ago

0.10.0-alpha.13

2 years ago

0.10.0-alpha.16

2 years ago

0.10.0-alpha.15

2 years ago

0.10.0-alpha.18

2 years ago

0.10.0-alpha.17

2 years ago

0.10.0-alpha.19

2 years ago

0.10.0-alpha.3

2 years ago

0.10.0-alpha.2

2 years ago

0.10.0-alpha.4

2 years ago

0.10.0-alpha.1

2 years ago

0.10.0-alpha.0

2 years ago

0.9.0-alpha.37

2 years ago

0.9.0-alpha.33

2 years ago

0.9.0-alpha.34

2 years ago

0.9.0-alpha.35

2 years ago

0.9.0-alpha.36

2 years ago

0.9.0

2 years ago

0.9.2

2 years ago

0.9.1

2 years ago

0.9.4

2 years ago

0.9.3

2 years ago

0.9.0-alpha.30

2 years ago

0.9.0-alpha.31

2 years ago

0.9.0-alpha.32

2 years ago

0.9.0-alpha.26

2 years ago

0.9.0-alpha.27

2 years ago

0.9.0-alpha.28

2 years ago

0.9.0-alpha.29

2 years ago

0.9.0-alpha.23

2 years ago

0.9.0-alpha.24

2 years ago

0.8.15

2 years ago

0.9.0-alpha.22

2 years ago

0.8.14

2 years ago

0.8.13

2 years ago

0.9.0-alpha.20

2 years ago

0.9.0-alpha.21

2 years ago

0.9.0-alpha.15

2 years ago

0.9.0-alpha.16

2 years ago

0.9.0-alpha.17

2 years ago

0.9.0-alpha.18

2 years ago

0.9.0-alpha.19

2 years ago

0.8.9

3 years ago

0.9.0-alpha.8

3 years ago

0.8.8

3 years ago

0.9.0-alpha.9

3 years ago

0.8.5

3 years ago

0.8.4

3 years ago

0.8.7

3 years ago

0.8.6

3 years ago

0.9.0-alpha.10

3 years ago

0.9.0-alpha.4

3 years ago

0.9.0-alpha.5

3 years ago

0.9.0-alpha.6

3 years ago

0.9.0-alpha.7

3 years ago

0.9.9-alpha.8

3 years ago

0.8.12

2 years ago

0.8.11

2 years ago

0.8.10

2 years ago

0.9.0-alpha.11

3 years ago

0.9.0-alpha.12

3 years ago

0.9.0-alpha.13

2 years ago

0.9.0-alpha.14

2 years ago

0.8.3

3 years ago

0.8.0-alpha.15

3 years ago

0.8.0-alpha.14

3 years ago

0.8.0-alpha.16

3 years ago

0.8.0-alpha.13

3 years ago

0.9.0-alpha.0

3 years ago

0.9.0-alpha.1

3 years ago

0.9.0-alpha.2

3 years ago

0.9.0-alpha.3

3 years ago

0.8.0-alpha.18

3 years ago

0.8.1

3 years ago

0.8.0

3 years ago

0.8.2

3 years ago

0.8.0-alpha.12

3 years ago

0.8.0-alpha.11

3 years ago

0.8.0-alpha.10

3 years ago