# Installation

{% hint style="info" %}
Before starting be aware that oidc-spa is not suited for Next.js.

If you are using Next the closer alternative is to use [NextAuth.js](https://next-auth.js.org/) (with [the Keycloak adapter](https://next-auth.js.org/providers/keycloak) if you are using Keycloak). See [this guide](https://phasetwo.io/docs/securing-applications/next/).
{% endhint %}

If you're having issues don't hesitate to [reach out on Discord](https://discord.gg/mJdYJSdcm4)!

## Add the lib to your dependencies

{% tabs %}
{% tab title="npm" %}

```bash
npm install oidc-spa
```

{% endtab %}

{% tab title="yarn" %}

```bash
yarn add oidc-spa
```

{% endtab %}

{% tab title="pnpm" %}

```bash
pnpm add oidc-spa
```

{% endtab %}

{% tab title="bun" %}

```bash
bun add oidc-spa
```

{% endtab %}
{% endtabs %}

## Editing your App entrypoint

This is optional but recommended for [better performances and security](/docs/v7/why-oidcearlyinit.md).

{% tabs %}
{% tab title="Vite" %}
First rename your entry point file from `main.tsx` (or `main.ts`) to `main.lazy.tsx`

```bash
mv src/main.tsx src/main.lazy.tsx
```

Then create a new `main.tsx` file:

{% code title="src/main.tsx" %}

```typescript
import { oidcEarlyInit } from "oidc-spa/entrypoint";

const { shouldLoadApp } = oidcEarlyInit({
    freezeFetch: true,
    freezeXMLHttpRequest: true,
    freezeWebSocket: true
});

if (shouldLoadApp) {
    // Note: Deferring the main app import adds a few milliseconds to cold start,
    // but dramatically speeds up auth. Overall, it's a net win.
    import("./main.lazy");
}
```

{% endcode %}
{% endtab %}

{% tab title="TanStack Start" %}
Comming soon, [follow progress](https://github.com/keycloakify/oidc-spa/issues/43).
{% endtab %}

{% tab title="React-Router Framework Mode" %}
You can skip this for now. It will be explained in the dedicated setup guide:

{% content-ref url="/pages/Gemwcbm0G6uqPI4c1liz" %}
[React Router](/docs/v7/setup-guides/react-router.md)
{% endcontent-ref %}
{% endtab %}

{% tab title="Create-React-App" %}
First rename your entry point file from `main.tsx` (or `main.ts`) to `main.lazy.tsx`

```bash
mv src/index.tsx src/index.lazy.tsx
```

Then create a new `index.tsx` file:

{% code title="src/index.tsx" %}

```typescript
import { oidcEarlyInit } from "oidc-spa/entrypoint";

const { shouldLoadApp } = oidcEarlyInit({
    freezeFetch: true,
    freezeXMLHttpRequest: true,
    freezeWebSocket: true
});

if (shouldLoadApp) {
    // Note: Deferring the main app import adds a few milliseconds to cold start,
    // but dramatically speeds up auth. Overall, it's a net win.
    import("./index.lazy");
}
```

{% endcode %}
{% endtab %}
{% endtabs %}


---

# 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.oidc-spa.dev/docs/v7/readme.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.
