inertiajs / inertia-laravel

The Laravel adapter for Inertia.js.
https://inertiajs.com
MIT License
2.1k stars 235 forks source link

[SSR] pass request headers to ssr server #665

Open MordiSacks opened 1 month ago

MordiSacks commented 1 month ago

It would be great if the HttpGateway will forward the original http context (headers, ip, etc) to the ssr renderer.

This would unlock client dependent opportunities (mobile detection, regional specific content, etc.) .

RobertBoes commented 1 month ago

Could you elaborate on why this is helpful and how this would work? I'm having a hard time understanding how this would work, while I get the value of it.

To be clear; Inertia's SSR would only render the HTML of the initial request, so it would only render HTML once, after that it's all CSR. This is useful for search engines and an optimization for load times. However, if you're conditionally rendering things based on the headers, that wouldn't work client-side, because that also doesn't receive request headers

MordiSacks commented 1 month ago

Let's look at view port for example

useViewPort.js

import {onMounted, reactive} from 'vue';

// no request context
const vp = reactive({w: 0, h: 0});

// with request context
const vp = reactive({w: parseUserAgent(req.headers.userAgent).type === 'mobile' ? 400 : 1024, h: 0});

export default function useViewPort() {
  if (typeof window !== 'undefined') onMounted(async () => {
    vp.w = window.innerWidth;
    vp.h = window.innerHeight;

    window.addEventListener('resize', () => {
      vp.w = window.innerWidth;
      vp.h = window.innerHeight;
    });
  });

  return vp;
}