Adding multiple HTTP headers?

I’m trying to implement HTTP/2 Server Push with CSS files (and eventually javascript & images as well).

From what I understand, I can’t add HTTP headers into my templates or snippets; I have to add it to my configs. (Please correct me if I’m wrong because this would make things a lot easier if possible!)

In my config, I have code like this:

c::set('headers', array(
	'default' => function($page) {
		header('Link: </assets/normalize.css>; rel=preload');
		header('Link: </assets/global.css>; rel=preload');
		header('Link: </assets/modules.css>; rel=preload');
	}
));

According to Chrome, only the last header in the code is properly doing server push.

Any ideas?

Okay, I just figured it out. They have to be listed in the same line:

c::set('headers', array(
	'default' => function($page) {
		header('Link: </assets/normalize.css>; rel=preload, </assets/global.css>; rel=preload, </assets/modules.css>; rel=preload');
	}
));

But now I have another question: is it possible to preload/server push a dynamic list of assets? For example, preload all .css files that are attached to the current page?

The headers option looks for the current template. So you could create separate rules for each template. And then each callback gets the $page variable like in your example. You can use it to check for specific values and output headers based on the information.

1 Like