Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load pages on runtime #2

Merged
merged 3 commits into from
Jul 3, 2022
Merged

Load pages on runtime #2

merged 3 commits into from
Jul 3, 2022

Conversation

ycs77
Copy link
Owner

@ycs77 ycs77 commented Jul 1, 2022

Supports the way to load pages on runtime (#1)

Usage

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-pluin.js
  └── pages/
     ├── Some.vue
     └── Other.vue

Use the InertiaPlugin runtime API in resources/js/my-runtime-pluin.js to load pages:

window.InertiaPlugin.addNamespace('MyRuntimePluin', 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-pluin.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-pluin.js) to public/vendor, and open app.blade.php to include scripts to load pages:

<head>
  <script src="https://unpkg.com/inertia-plugin@^0.4" defer></script>
  <script src="/vendor/my-runtime-pluin/js/my-runtime-pluin.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:

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

Over, using pages:

Inertia::render('MyRuntimePluin::Some');

@ycs77 ycs77 linked an issue Jul 1, 2022 that may be closed by this pull request
@ycs77 ycs77 changed the title Runtime mode Load plugins on runtime Jul 1, 2022
@ycs77 ycs77 changed the title Load plugins on runtime Load pages on runtime Jul 3, 2022
@ycs77 ycs77 force-pushed the runtime branch 2 times, most recently from 42457d2 to fd45570 Compare July 3, 2022 10:48
@ycs77 ycs77 marked this pull request as ready for review July 3, 2022 10:51
@ycs77 ycs77 merged commit 0f2ace7 into main Jul 3, 2022
@ycs77 ycs77 deleted the runtime branch July 3, 2022 10:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a way to load plugin on runtime
1 participant