Giter Club home page Giter Club logo

directus-extensions's Introduction

Directus Extension SSR logo


Directus SSR ๐Ÿฐ โšก

Render your Vite Application next to Directus: easy as hell.

directus-extension-ssr@^2.0.0 => @directus/sdk@^11.0.0
[email protected] => @directus/sdk@^10.3.0

Installation

Install directus-extension-ssr into your self-hosted directus instance.

cd /path/to/your/directus/installation
npm install directus-extension-ssr

Setup Vite Project

FIRST STEP

Setup your vite project like you normaly do. (Directly in your directus self-hosted project).
But your src/main.ts file should look similar like this:

import { handler } from 'directus-extension-ssr'
import App from './App.vue'

export default handler(App, {
  routerType: 'vue-router', // or 'unplugin-vue-router'
  routerOptions: {
    routes: [
      { path: '/', component: () => import('./pages/Home.vue') },
      { path: '/about', component: () => import('./pages/About.vue') },
    ],
    // extendRoutes: () => setupLayouts(routes) => if you want to use 'unplugin-vue-router' with 'vite-plugin-vue-layouts'! But don not forget pnpm i -D vite-plugin-vue-layouts
  }
}, async (ctx) => {
  // Add your custom logic here
  // register pinia for example
  // register vuetify
  // do what ever you want
},)

SECOND STEP

Next add DirectusSSR in vite.config.ts as a plugin.

import path from 'node:path'
import { defineConfig } from 'vite'
import Vue from '@vitejs/plugin-vue'
import DirectusSSR from 'directus-extension-ssr/plugin'

export default defineConfig({
  plugins: [
    DirectusSSR(),
  ],
})

THIRD STEP

Disable directus ROOT_REDIRECT through your directus .env / config.js / config.ts whatever

[...]
ROOT_REDIRECT=false
[...]

FOURTH STEP

Be sure, that your index.html includes the following COMMENTS (--app-html-- and --preload-links--) exactly as shown below:

<!DOCTYPE html>
<html lang="en">
<head>
  <!--preload-links-->
</head>
<body>
  <div id="app"><!--app-html--></div>
  <script type="module" src="/src/main.ts"></script>
</body>
</html>

Inject Directus

Just use inject('directus') anywhere in setup function.

Pinia

If you want to use Pinia, do something like that:

// src/modules/pinia.ts
import { directusSSRPlugin } from 'directus-extension-ssr/utils'

export const install: UserModule = (ctx) => {
  const pinia = createPinia()

  pinia.use(p => directusSSRPlugin(p, ctx))

  ctx.app.use(pinia)
}

// src/pinia.d.ts
declare module 'pinia' {
  export interface PiniaCustomProperties {
    directus: import('directus-extension-ssr/types').AppDirectusClient
  }
}

// src/main.ts
export default handler(App, {
  routerOptions: {
    routes: [
      { path: '/', component: () => import('./pages/Home.vue') }
    ],
    // or
    // routes: setupLayouts(routes)
  }
}, async (ctx) => {
  // install all modules under `modules/`
  Object.values(import.meta.glob < { install: UserModule } > ('./modules/*.ts', { eager: true }))
    .forEach(i => i.install?.(ctx))
})

Dev

You can set the environment variable SSR_ENV to "development" through your directus .env / config.js / config.ts file or via cli.
Have fun with HMR ๐Ÿ”ฅ

SSR_ENV=development npx directus start

Build

Like in any other vite ssr project, extend your package.json with the following vite build scripts:

"scripts": {
  "build": "pnpm run build:client && pnpm run build:server",
  "build:client": "vite build --outDir dist/client --ssrManifest",
  "build:server": "vite build --outDir dist/server --ssr src/main.ts"
}

Prod

after build - run:

npx directus start

easy as hell ๐Ÿ˜ˆ

Try it

Check out: examples/app

git clone [email protected]:domsen123/directus-extension-ssr.git
cd directus-extension-ssr
pnpm build && pnpm app:bootstrap

dev mode:

pnpm app:dev

prod mode:

pnpm app:build && pnpm app:start

Got to example application: Application
Go to directus admin dashboard: Directus Dashboard

directus-extensions's People

Contributors

domsen123 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.