k_nadam's avatar

Preloading images via injecting preload link in response header

Hello,

I want to preload images on my Laravel application before rendering the Vue template, so I worked on this middleware

public function handle(Request $request, Closure $next): Response
    {
        $response = $next($request);
        
        if ($request->header('X-Inertia')) {
            return $response;
        }

        $preloads = $request->attributes->get('preload_images', []);

        foreach ($preloads as $url) {
            $response->headers->set(
                'Link',
                "<{$url}>; rel=preload; as=image; fetchpriority=high",
                false
            );
        }

        return $response;
    }

Which sets preloads links on the response header. it worked fine. the problem is that it is re-loading the image after rendering the vue page.

Is there a way to get around this?

Thank you.

0 likes
2 replies
k_nadam's avatar

Yes. this works great if you are navigating between inertia pages. however if you go to the page and reload it, it will not preload the image.

JussiMannisto's avatar

the problem is that it is re-loading the image after rendering the vue page.

Is it actually reloading the image from the server, or just loading it from the local cache? Can you show what happens in your network tab?

however if you go to the page and reload it, it will not preload the image.

How did you determine that?

Why are you not sending the header for Inertia requests?

Please or to participate in this conversation.