Disable SSR for some pages #1429
Replies: 5 comments 7 replies
-
This worked for me, thanks! |
Beta Was this translation helpful? Give feedback.
-
Here is, I think, a better alternative : Create a new HTTP Gateway where you want (e.g app folder): namespace App\Inertia;
use Illuminate\Support\Str;
use Inertia\Ssr\HttpGateway;
use \Inertia\Ssr\Response;
class InertiaHttpGateway extends HttpGateway
{
/**
* Dispatch the Inertia page to the Server Side Rendering engine.
*
* @param array $page
* @return Response|null
*/
public function dispatch(array $page): ?Response
{
if (isset($page['url']) && Str::is('/dashboard*', $page['url'])) {
return null;
}
return parent::dispatch($page);
}
} Last step, register the binding to your custom HTTP Gateway in your AppServiceProvider: use App\Inertia\InertiaHttpGateway;
use Inertia\Ssr\HttpGateway;
class AppServiceProvider extends ServiceProvider
{
public $bindings = [
HttpGateway::class => InertiaHttpGateway::class,
];
} Hope this helps. |
Beta Was this translation helpful? Give feedback.
-
My recommendation is to use the if (Request::is('nova/*')) {
Config::set('inertia.ssr.enabled', false);
} |
Beta Was this translation helpful? Give feedback.
-
These solutions cause the page to log errors
I assume due to |
Beta Was this translation helpful? Give feedback.
-
Currently I'm doing a workaround like this (inspired by @valh1996 answer's) - Folder and files structure│ welcome.tsx
├───admin
│ │ dashboard.tsx
│ │ index.tsx
│ ├───auth
│ │ confirm-password.tsx
│ │ ...
├───customer.ssr // 👈 every page inside this folder will render on server
│ │ home.tsx
│ └───auth
│ login.tsx
└───delivery
│ index.ssr.tsx // 👈 only this page will render on server
│ orders.tsx
Prevent inertia from rendering page on server// app\Http\Inertia\Gateway.php
use Inertia\Ssr\HttpGateway;
class Gateway extends HttpGateway
{
public function dispatch(array $page): ?Response
{
// checks if page has `ssr` named on the
// current page component
if (
isset($page) &&
! Str::contains($page['component'],'ssr')
) {
return null;
}
return parent::dispatch($page);
}
}
// app\Providers\AppServiceProvider.php
use Inertia\Ssr\HttpGateway;
use App\Http\Inertia\Gateway;
class AppServiceProvider extends ServiceProvider
{
public $bindings = [
HttpGateway::class => Gateway::class,
];
} Let bundler know only build javascript pages for SSR which are marked ssr// resources\js\ssr.tsx
createServer((page) =>
createInertiaApp({
...
resolve: (name) => resolvePageComponent(
`./pages/${name}.tsx`,
// 👇 pages for ssr
import.meta.glob([
'./pages/**/*.ssr*/**/*.tsx',
'./pages/**/*.ssr.tsx',
]),
),
setup: ({ App, props }) => {...},
})
); And at last hydrate or create root in client side setup// resources\js\app.tsx
createInertiaApp({
...
setup({ el, App, props }) {
if (props.initialPage.component.includes('ssr')) {
hydrateRoot(el, <App {...props} />);
} else {
createRoot(el).render(<App {...props} />);
}
},
...
}); |
Beta Was this translation helpful? Give feedback.
-
I realized that there is not documentation or anything for disabling SSR for some routes.
I have solved this with a workaround for now.
Add this before
@inertiaHead
in the blade layout fileapp.blade.php
:If someone has a better alternative, please share!
Beta Was this translation helpful? Give feedback.
All reactions