> For the complete documentation index, see [llms.txt](https://docs.payengine.co/developer-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.payengine.co/developer-docs/getting-started-1/frontend-frameworks/preloading-web-components.md).

# 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({
    publicKey: 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.&#x20;

### 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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
