Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

demonz's avatar

I want to hide data-page of inertia js

Hi , I am using laravel, inertia js and react js. I setup the SSR too. I want to hide data-page from displaying in the browser Elements and Page Source. App.jsx

createInertiaApp({
    title: (title) => `${title} - ExampleApp`,
    resolve: (name) => resolvePageComponent(`./Pages/${name}.jsx`, import.meta.glob('./Pages/**/*.jsx')),
    setup({ el, App, props }) {
        hydrateRoot(el, <App {...props} />)
    },
    progress: {
        color: '#4B5563',
    },
});

SSR.jsx

createServer((page) =>
    createInertiaApp({
        page,
        render: ReactDOMServer.renderToString,
        title: (title) => `${title} - ExampleApp`,
        resolve: (name) => resolvePageComponent(`./Pages/${name}.jsx`, import.meta.glob('./Pages/**/*.jsx')),
        setup: ({ App, props }) => {
            global.route = (name, params, absolute) =>
                route(name, params, absolute, {
                    ...page.props.ziggy,
                    location: new URL(page.props.ziggy.location),
                });

            return <App {...props} />;
        },
    })
);

HandleInertiaRequests

    public function share(Request $request)
    {
        return array_merge(parent::share($request), [
            'auth' => [
                'user' => $request->user(),
            ],
            'ziggy' => function () use ($request) {
                return array_merge((new Ziggy)->toArray(), [
                    'location' => $request->url(),
                ]);
            },
            'flash' => function () use ($request) {
                return [
                    'success' => $request->session()->get('success'),
                    'error' => $request->session()->get('error'),
                ];
            },
        ]);
    }
0 likes
6 replies
projectasphaliea's avatar

Personally I do it like this.

app.js

createInertiaApp({
    // config
}).then(() => {
    document.getElementById('app').removeAttribute('data-page');
});

ssr.js

createServer((page) => createInertiaApp({
    // config
}).then((app) => {
    const appRender = app;
    appRender.body = appRender.body.replace(/data-page=".*?"/, '');
    return appRender;
}));

This will remove the data-page attribute after rendering the page.

Just remind, the data has to be send to the page, because that's how Inertia works. Do NOT send any sensitive information to the front-end which you're trying to hide. I just like a clean document code which is the reason why I am doing it.

1 like
martinbean's avatar

@demonz You can’t. That’s literally how Inertia works.

If you’re worried about sensitive data being in there, then don’t send sensitive data client side in the first place. Use something like Eloquent API resources to only send the attributes you need to the client side.

1 like
demonz's avatar

@martinbean hi how about ziggy routes is it possible to remove them from data-page ?

martinbean's avatar

@demonz Again, just don’t send anything to the front end that you don’t want in the front end in the first place.

projectasphaliea's avatar

@demonz Try the solution I posted, this does exactly that. I used that because I find the data-page attribute ugly. But like @martinbean said, don't send any sensitive information to the front-end because users will still be able to see this in the XHR requests.

Please or to participate in this conversation.