This is the documentation for v9, checkout the latest version
Keycloakify
HomeGitHubStartersStorybookDiscordKeycloak-js Alternative
v9
  • Keycloakify
  • Release Notes & Upgrade Instructions
  • FAQ
v9
  • ๐Ÿ‘จโ€๐Ÿ’ปQuick start
  • ๐Ÿ“ฅImporting your theme in Keycloak
  • ๐Ÿ”ฉKeycloakify in my App
  • ๐Ÿ–ผ๏ธImporting assets and fonts
  • ๐Ÿ“งEmail customization
  • โœ’๏ธTerms and conditions
  • โœ…Realtime input validation and custom registration fields
  • โš ๏ธLimitations
  • ๐Ÿ”งEnvironment Variables
  • ๐ŸŒŽi18n: msg(...)
  • ๐Ÿ“–Build options
  • ๐Ÿ’‚Email domain acceptlist
  • ๐Ÿ›‘Keycloak error in log
  • ๐ŸŒ‰Passing values from the App to the theme
  • ๐Ÿ’ŸContributing
  • ๐Ÿค”How it works
  • โฌ†๏ธMigration guides
    • โฌ†๏ธCRA -> Vite
    • โฌ†๏ธv8 -> v9
    • โฌ†๏ธv7 -> v8
    • โฌ†๏ธv6 -> v7
    • โฌ†๏ธv6.x -> v6.12
    • โฌ†๏ธv5 -> v6
Powered by GitBook
On this page
  • Importing Custom assets that aren't fonts
  • Importing Fonts
  • Importing Default Keycloak Theme Resources

Was this helpful?

Importing assets and fonts

PreviousKeycloakify in my AppNextEmail customization

Last updated 27 days ago

Was this helpful?

Importing Custom assets that aren't fonts

For example, you can place foo.png in src/login/assets and import it in the template as shown below:

src/login/Template.tsx
import fooPngUrl from "./assets/foo.png";
// NOTE: fooPngUrl is a URL (string) that points to the asset.

// ...

<img src={fooPngUrl} />

This is how you would reliably import assets that you have in your public directory regardless of if you are in a Keycloak context or not.

Let's assume you have foo.png in the public/ directory. To import it you would do.

In you public/index.html file

public/index.html
<!DOCTYPE html>
<html>
<head>
  <link rel="icon" href="/foo.png" />  
</head>

<body>
  <div id="root"></div>
</body>

</html>

In your TypeScript files

src/login/Template.tsx
// Note: Base url is often just "/" but please use the BASE_URL env
// as it enable Keycloakify to resolve your assets when in Keycloak.   
<img src={`${import.meta.env.BASE_URL}foo.png`} />

This is how you would reliably import assets that you have in your public directory regardless of if you are in a Keycloak context or not.

Let's assume you have foo.png in the public/ directory. To import it you would do.

In you public/index.html file

public/index.html
<!DOCTYPE html>
<html>
<head>
  <link rel="icon" href="%PUBLIC_URL%/foo.png" />  
</head>

<body>
  <div id="root"></div>
</body>

</html>

In your TypeScript files

src/login/Template.tsx
// This is like process.env.PUBLIC_URL but it works both inside
// and outside of Keycloak.  
import { PUBLIC_URL } from "keycloakify/PUBLIC_URL";

<img src={`${PUBLIC_URL}/foo.png`} />

Importing Fonts

So, this is typically how you would import self hosted font in a Regular React App. Let's imagine you have the following font index CSS file:

public/fonts.css
@font-face {
  font-family: Marianne;
  src: url("./fonts/Marianne-Light.woff2") format("woff2");
  font-weight: 300;
  font-style: normal;
  font-display: swap;
}

You would import it like this:

public/index.html
<!-- ๐Ÿ›‘ This do not work in keycloakfy --> 
<link rel="stylesheet" href="/font.css" />

Unfortunately this approach does not work in Keycloakify.

The workaround consist in including all your @font-face statements directly in your public/index.html file. This is how you would update your index.html file in order to make it work with Keycloakify:

public/index.html
-<link rel="stylesheet" href="/font.css" />
+<style>
+ @font-face {
+   font-family: Marianne;
+   src: url("%BASE_URL%fonts/Marianne-Light.woff2") format("woff2");
+   font-weight: 300;
+   font-style: normal;
+   font-display: swap;
+ }
+</style>

So, this is typically how you would import self hosted font in a Regular React App. Let's imagine you have the following font index CSS file:

public/fonts.css
@font-face {
  font-family: Marianne;
  src: url("./fonts/Marianne-Light.woff2") format("woff2");
  font-weight: 300;
  font-style: normal;
  font-display: swap;
}

You would import it like this:

public/index.html
<!-- ๐Ÿ›‘ This do not work in keycloakfy --> 
<link rel="stylesheet" href="%PUBLIC_URL%/font.css" />

Unfortunately this approach does not work in Keycloakify.

The workaround consist in including all your @font-face statements directly in your public/index.html file. This is how you would update your index.html file in order to make it work with Keycloakify:

public/index.html
-<link rel="stylesheet" href="%PUBLIC_URL%/font.css" />
+<style>
+ @font-face {
+   font-family: Marianne;
+   src: url("%PUBLIC_URL%/fonts/Marianne-Light.woff2") format("woff2");
+   font-weight: 300;
+   font-style: normal;
+   font-display: swap;
+ }
+</style>

Works out of the box โœ…

Importing Default Keycloak Theme Resources

This section is mainly for transparency. You should use your own assets instead of importing the default ones; that's the whole point of creating a custom theme.

To import resources available in the default theme, you can construct URLs like:

const patternflyUrl = `${kcContext.url.resourcesCommonPath}/node_modules/patternfly/dist/css/patternfly.min.css`;
const loginCssUrl = `${url.resourcesPath}/css/login.css`;

This is not recommended, Keycloakify or not, whenever possible prefer importing your assets using the import statement. .

This is not recommended, Keycloakify or not, whenever possible prefer importing your assets using the import statement. .

Example (and the font are ).

Storybook: To have your font applied in your Storybook as well you need to import them as shown .

Example (and the font are ).

Storybook: To have your font applied in your Storybook as well you need to import them as shown .

You can see what assets are available under public/keycloak-resources/login/resources. If you want to choose which version of the assets to use, refer to .

By default, the default CSS assets are imported and applied in the login theme.

๐Ÿ–ผ๏ธ
Learn more
Learn more
here
here
here
here
here
here
this build option
here
Demo how to import image in React ยท keycloakify/keycloakify-starter@c3e7067GitHub
Demo in the Starter project
https://github.com/keycloakify/keycloakify-starter/blob/d28c986686c3255b812152f68f3458995fa8bfb7/public/index.html#L8-L17
Example from the starter: Import favicon
Example in the starter
Show how to import from public/ ยท keycloakify/keycloakify-starter@7a35896GitHub
Logo
Logo
Keycloakify correctly replaces the URLs
The favicon is correctly loaded for the Keycloak server
The asset is successfully downloaded
The favicon is correctly loaded for the Keycloak server
The asset is successfully downloaded