Inertia Plugin
Warning: This plugin status is experimental, don't use it for the production application.
The plugin page loader for Inertia.js, that allows the server-side to use Inertia::render('my-package::Page');
.
Install
npm i inertia-plugin -D
Vite
// vite.config.js
import Inertia from 'inertia-plugin/vite'
export default defineConfig({
plugins: [
Inertia({ /* options */ }),
],
})
Webpack
// webpack.config.js
const inertiaPlugin = require('inertia-plugin/webpack').default
module.exports = {
/* ... */
plugins: [
inertiaPlugin({ /* options */ })
]
}
Laravel Mix
// webpack.mix.js
const inertiaPlugin = require('inertia-plugin/webpack').default
mix
.webpackConfig({
plugins: [
inertiaPlugin({ /* options */ }),
],
})
Type
Add to env.d.ts
:
/// <reference types="inertia-plugin/client" />
Configuration
Inertia({
// Current work directory
cwd: process.cwd(),
// Define namespace mapping
namespaces: [],
// Namespace separator
separator: '::',
// Module extension
// (Defaults to '.vue' if not set and used with vite)
extension: '',
// Use `import()` to load pages for webpack, default is using `require()`
// Only for webpack
import: false,
// Enable SSR mode
ssr: false,
})
Usage with Vite
Add inertia-plugin
to vite.config.js
:
import Inertia from 'inertia-plugin/vite'
export default defineConfig({
plugins: [
Inertia({
namespaces: [
{ 'my-package-1': 'node_modules/my-plugin1/src/Pages' },
{ 'my-package-2': 'node_modules/my-plugin2/src/Pages' },
],
}),
],
})
And use resolvePage()
in resources/js/app.js
to resolve the app pages and namespaced pages (don't use one line function):
import { resolvePage } from '~inertia'
createInertiaApp({
resolve: resolvePage(() => {
return import.meta.glob('./pages/**/*.vue')
}),
})
Or you can add persistent layout:
import Layout from './Layout'
createInertiaApp({
resolve: resolvePage(name => {
return import.meta.glob('./pages/**/*.vue')
}, page => {
page.layout = Layout
return page
}),
})
Usage with Laravel Mix
Add inertia-plugin
to webpack.mix.js
:
mix
.webpackConfig({
plugins: [
inertiaPlugin({
namespaces: ({ npm, composer }) => [
{ 'my-package-1': 'node_modules/my-plugin1/src/Pages' },
{ 'my-package-2': 'node_modules/my-plugin2/src/Pages' },
],
}),
],
})
And use resolvePage()
in resources/js/app.js
to resolve the app pages and namespaced pages:
import { resolvePage } from '~inertia'
createInertiaApp({
resolve: resolvePage(name => require(`./pages/${name}`)),
})
Or you can add persistent layout:
import Layout from './Layout'
createInertiaApp({
resolve: resolvePage(name => require(`./pages/${name}`), page => {
page.layout = Layout
return page
}),
})
Load namespace in package
If you create a plugin with Inertia pages, the plugin user can use the function npm()
or composer()
to load the namespace:
export default defineConfig({
plugins: [
Inertia({
namespaces: ({ npm, composer }) => [
// define namespace mapping:
{ 'my-package-1': 'node_modules/my-plugin1/src/Pages' },
{ 'my-package-2': 'node_modules/my-plugin2/src/Pages' },
// load namespace from npm package:
npm('my-plugin2'),
// load namespace from composer package:
composer('ycs77/my-php-package'),
],
}),
],
})
If you created is npm package, must be added the inertia
field to define the namespace mapping, for example in node_modules/my-plugin2/package.json
, and you would put pages into src/other-pages
directory:
{
"name": "my-plugin2",
"inertia": {
"my-package-2": "src/other-pages"
}
}
If you created is composer package, must be added the extra.inertia
field to define the namespace mapping, for example in vendor/ycs77/my-php-package/composer.json
, and you would put pages into resources/js/Pages
directory:
{
"name": "ycs77/my-php-package",
"extra": {
"inertia": {
"my-php-package": "resources/js/Pages"
}
}
}