Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
inertia-plugin
Advanced tools
The plugin page loader for Inertia.js, that allows the server-side to use Inertia::render('my-package::Page');
.
First, install the Inertia Plugin to your main Inertia app:
npm i inertia-plugin -D
// vite.config.js
import Inertia from 'inertia-plugin/vite'
export default defineConfig({
plugins: [
Inertia({ /* options */ }),
],
})
// webpack.config.js
const InertiaPlugin = require('inertia-plugin/webpack')
module.exports = {
/* ... */
plugins: [
InertiaPlugin({ /* options */ }),
],
}
// webpack.mix.js
const InertiaPlugin = require('inertia-plugin/webpack')
mix
.webpackConfig({
plugins: [
InertiaPlugin({ /* options */ }),
],
})
Add to env.d.ts
:
/// <reference types="inertia-plugin/client" />
This package supports the Static and Runtime to load the pages (can be mixed to use), so you can select the way to build and use your Inertia pages:
Then select the source from which you want to load the page:
If you created or have a package, you can select the build tool to use the package:
You must create an npm package that contains the pages
folder:
src/pages/
├── Some.vue
└── Dir/
└── Other.vue
And added the inertia
field to define the namespace mapping, for example in node_modules/my-plugin/package.json
:
{
"name": "my-plugin",
"inertia": {
"my-package": "src/pages"
}
}
Publish this package and back to the main Inertia app to install this package:
npm i my-plugin
Next step you can select the build tool to use:
You must create a composer package that contains the pages
folder:
resources/js/pages/
├── Some.vue
└── Dir/
└── Other.vue
And added the extra.inertia
field to define the namespace mapping, for example in vendor/ycs77/my-php-package/composer.json
:
{
"name": "ycs77/my-php-package",
"extra": {
"inertia": {
"my-php-package": "resources/js/pages"
}
}
}
Publish this package and back to the main Inertia app to install this package:
composer require ycs77/my-php-package
Next step you can select the build tool to use:
Add inertia-plugin
to vite.config.js
, and you can use the function npm()
or composer()
to load the namespace:
import Inertia from 'inertia-plugin/vite'
export default defineConfig({
plugins: [
Inertia({
namespaces: ({ npm, composer }) => [
// load namespace from npm package:
npm('my-plugin'),
// load namespace from composer package:
composer('ycs77/my-php-package'),
],
}),
],
})
And use resolvePage()
in resources/js/app.js
to resolve the app pages and npm / composer pages (don't use one line function):
import { resolvePage } from '~inertia'
createInertiaApp({
resolve: resolvePage(() => {
return import.meta.glob('./pages/**/*.vue', { eager: true })
}),
})
Or you can add the persistent layout:
import Layout from './Layout'
createInertiaApp({
resolve: resolvePage(name => {
return import.meta.glob('./pages/**/*.vue', { eager: true })
}, page => {
page.layout = Layout
return page
}),
})
Now you can use the pages:
Inertia::render('my-package::Some'); // in npm package
Inertia::render('my-php-package::Some'); // in composer package
Add inertia-plugin
to webpack.mix.js
, and you can use the function npm()
or composer()
to load the namespace:
mix
.webpackConfig({
plugins: [
inertiaPlugin({
namespaces: ({ npm, composer }) => [
// load namespace from npm package:
npm('my-plugin'),
// load namespace from composer package:
composer('ycs77/my-php-package'),
],
}),
],
})
And use resolvePage()
in resources/js/app.js
to resolve the app pages and npm / composer pages:
import { resolvePage } from '~inertia'
createInertiaApp({
resolve: resolvePage(name => require(`./pages/${name}`)),
})
Or you can add the persistent layout:
import Layout from './Layout'
createInertiaApp({
resolve: resolvePage(name => require(`./pages/${name}`), page => {
page.layout = Layout
return page
}),
})
Now you can use the pages:
Inertia::render('my-package::Some'); // in npm package
Inertia::render('my-php-package::Some'); // in composer package
If you use the modules package to manage your Laravel application, such as Laravel Modules, you can also define namespace mapping:
Note: Of course, can also be load pages from other locations in the main application.
export default defineConfig({
plugins: [
Inertia({
namespaces: [
// define namespace mapping:
{ 'my-module': 'Modules/MyModule/Resources/js/pages' },
// define more namespace mapping:
{
'my-module-2': 'Modules/MyModule2/Resources/js/pages',
'special-modal': 'resources/js/SpecialModals',
},
],
}),
],
})
Now you can use the pages:
Inertia::render('my-module::Some');
Inertia::render('my-module-2::Some');
Inertia::render('special-modal::VeryCoolModal');
Sometimes you may want users to use the pages without compiling them after installing the composer package, at this time you can load them at runtime. This is the package directory structure:
resources/js/
├── my-runtime-plugin.js
└── pages/
├── Some.vue
└── Other.vue
Use the InertiaPlugin runtime API in resources/js/my-runtime-plugin.js
to load pages:
window.InertiaPlugin.addNamespace('my-runtime', name => require(`./Pages/${name}`))
And setting webpack.mix.js
to build assets:
const mix = require('laravel-mix')
mix
.setPublicPath('public')
.js('resources/js/my-runtime-plugin.js', 'public/js')
.vue({ runtimeOnly: true })
.version()
.disableNotifications()
Now you can publish this package and install it in the Inertia app, publish assets (my-runtime-plugin.js
) to public/vendor/inertia-plugins
, and open app.blade.php
to include scripts to load pages:
<head>
<script src="https://cdn.jsdelivr.net/npm/inertia-plugin@^0.4.0"></script>
<script src="/vendor/inertia-plugins/my-runtime-plugin.js" defer></script>
<!-- app.js must be last one -->
<script src="{{ mix('/js/app.js') }}" defer></script>
</head>
But the app.js
must build with inertia-plugin
, you can follow Install chapter to install it (does not need to include any option), like this:
// vite.config.js
import Inertia from 'inertia-plugin/vite'
export default defineConfig({
plugins: [
Inertia(),
],
})
Or using in Laravel Mix:
// webpack.mix.js
const InertiaPlugin = require('inertia-plugin/webpack')
mix
.webpackConfig({
plugins: [
InertiaPlugin(),
],
})
Over, using pages:
Inertia::render('my-runtime::Some');
Inertia({
// Current work directory.
cwd: process.cwd(),
// Define namespace mapping.
namespaces: [],
// Namespace separator.
separator: '::',
// Module extensions.
extensions: '',
// extensions: '', // webpack default
// extensions: 'vue', // webpack example
// extensions: 'vue', // vite default
// extensions: ['vue', 'js'], // vite example
// Use `import()` to load pages for webpack, default is using `require()`.
// Only for webpack.
import: false,
// Enable SSR mode.
ssr: false,
})
If you think this package has helped you, please consider Becoming a sponsor to support my work~ and your avatar will be visible on my major projects.
FAQs
The plugin page loader for Inertia.js
We found that inertia-plugin demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.