# 6a. Preloading PayEngine Web-Component in SPA

To ensure fast performance and a seamless user experience, it's important to preload the PayEngine Web-Components as early as possible in your Single Page Application (SPA). This allows PayEngine to initialize the required scripts before the user interacts with any UI that depends on them.

### How to Preload

Use the loadPayengine method during the early lifecycle of your app (e.g., in your root component or app bootstrap logic):

```javascript
loadPayengine({
    public_key: config.PAYENGINE_PUBLIC_KEY,
    //lightweight: true // Optional
}).then((pe) => {
    console.log('PayEngine initialized', pe);
});
```

### Understanding the lightweight Option

The lightweight option controls how much of the PayEngine Web-Components library is loaded:

* ***lightweight:true*** - Only loads the core components required for **accepting payments,** including Secure Fields and other essential scripts. This mode is optimized for speed and ideal if you only need basic payment functionality.
* ***Full mode (default)*** - If you do **not** specify lightweight, the full set of PayEngine Web-Components will be loaded. This includes **everything needed for payments** plus additional embedded web components like transaction history.

### When to Use Each Mode

<table><thead><tr><th width="188.013671875">Mode</th><th>Use When...</th></tr></thead><tbody><tr><td>lightweight:true</td><td>You only need payment functionality (e.g., card entry, submit payment).</td></tr><tr><td>Full mode (default)</td><td>You also need access to embedded web components like transaction history etc.</td></tr></tbody></table>

### Best Practices

* **Always preload** the PayEngine Web-Components at the earliest possible point in your app lifecycle to reduce load time during user interaction.
* Make sure to **await the Promise** returned by loadPayengine( ) if other parts of your app rely on the components being fully initialized.
* For performance-sensitive flows, consider starting with lightweight: true and loading additional components dynamically only if/when needed.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.payengine.co/developer-docs-v2.5/getting-started-1/frontend-frameworks/preloading-web-components.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
