Post by letxobnav » Wed Feb 27, 2019 10:59 pm

If you have implemented HTTP/2 on your host, you can push assets more effectively.

https://developers.google.com/web/funda ... nce/http2/

for opencart V3 you can add this to the system/library/response.php class after

Code: Select all

if ($this->output) {
it will send the push headers for js,css,fonts,images and mp3's if you wish.
Either individual (per asset), per asset type or all consolidated in one header.

Code: Select all

// HTTP/2 push asset headers

// Which assets to push
$pushCss = true;
$pushJs = true;
$pushFont = false;
$pushImage = true;
$pushMp3 = true;

// All assets in a single link header, overrides $pushIndividual
$pushConsolidate = false;
// Each asset in a separate link header otherwise one link per asset type
$pushIndividual = false;


// Array declarations
$headersJs = array();
$headersCss = array();
$headersFont = array();
$headersImage = array();
$headersMp3 = array();
$headersAll = array();

// Find the assets in the output, css, javascript, fonts, audio and images
$regexp = '#(src|href)="([^"]+\.(css|js|png|svg|gif|jpg|webp|woff2|mp3)(\?[^"]+)?)"#';
if (preg_match_all($regexp, $this->output, $matches, PREG_SET_ORDER)) {
	foreach ($matches as $match) {
		$file = $match[2];
		$type = $match[3];
		if ($type === 'js') {
			if ($pushJs) $headersJs[] = sprintf('<%s>; rel=preload; as=%s', $file, 'script');
		} elseif ($type === 'css') {
			if ($pushCss) $headersCss[] = sprintf('<%s>; rel=preload; as=%s', $file, 'style');
		} elseif ($type === 'woff2') {
			if ($pushFont) $headersFont[] = sprintf('<%s>; rel=preload; as=%s', $file, 'font');
		} elseif ($type === 'mp3') {
			if ($pushMp3) $headersMp3[] = sprintf('<%s>; rel=preload; as=%s', $file, 'audio');
		} else {
			if ($pushImage) $headersImage[] = sprintf('<%s>; rel=preload; as=%s', $file, 'image');
		}
	}
	$matches = null;unset($matches);
	if ($pushConsolidate) {
		$headersAll = array_merge($headersCss,$headersJs,$headersFont,$headersImage,$headersMp3);
		$headersAll = array_unique($headersAll);
		$headersAll = implode(',',$headersAll);
		if ($headersAll) header('Link: '.$headersAll, false);
	} else {
		if ($pushCss) {
			$headersCss = array_unique($headersCss);
			if ($pushIndividual) {
				foreach($headersCss as $headerCss) header('Link: '.$headerCss, false);
			} else {
				$headersCss = implode(',',$headersCss);
				if ($headersCss) header('Link: '.$headersCss, false);
			}
		}
		if ($pushJs) {
			$headersJs = array_unique($headersJs);
			if ($pushIndividual) {
				foreach($headersJs as $headerJs) header('Link: '.$headerJs, false);
			} else {
				$headersJs = implode(',',$headersJs);
				if ($headersJs) header('Link: '.$headersJs, false);
			}
		}
		if ($pushFont) {
			$headersFont = array_unique($headersFont);
			if ($pushIndividual) {
				foreach($headersFont as $headerFont) header('Link: '.$headerFont, false);
			} else {
				$headersFont = implode(',',$headersFont);
				if ($headersFont) header('Link: '.$headersFont, false);
			}
		}
		if ($pushImage) {
			$headersImage = array_unique($headersImage);
			if ($pushIndividual) {
				foreach($headersImage as $headerImage) header('Link: '.$headerImage, false);
			} else {
				$headersImage = implode(',',$headersImage);
				if ($headersImage) header('Link: '.$headersImage, false);
			}
		}
		if ($pushMp3) {
			$headersMp3 = array_unique($headersMp3);
			if ($pushIndividual) {
				foreach($headersMp3 as $headerMp3) header('Link: '.$headerMp3, false);
			} else {
				$headersMp3 = implode(',',$headersMp3);
				if ($headersMp3) header('Link: '.$headersMp3, false);
			}
		}
	}
}
// HTTP2 push headers

Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


User avatar
Expert Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan

Post by straightlight » Wed Feb 27, 2019 11:39 pm

The downside of these codes is that loops are involved with headers redirection while only one header at a time should rather be used to avoid redirection loops on the browser. In addition, the response.php file library should not handle these calls since Opencart uses the API as remote APis might also be affected with those redirections.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by letxobnav » Thu Feb 28, 2019 1:18 am

not sure what redirection headers you are babbling about here.

Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


User avatar
Expert Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan

Post by straightlight » Thu Feb 28, 2019 2:47 am

Well, of course, let's take a look:

Code: Select all

// HTTP/2 push asset headers

// Which assets to push
$pushCss = true;
$pushJs = true;
$pushFont = false;
$pushImage = true;
$pushMp3 = true;

// All assets in a single link header, overrides $pushIndividual
$pushConsolidate = false;
// Each asset in a separate link header otherwise one link per asset type
$pushIndividual = false;


// Array declarations
$headersJs = array();
$headersCss = array();
$headersFont = array();
$headersImage = array();
$headersMp3 = array();
$headersAll = array();

// Find the assets in the output, css, javascript, fonts, audio and images
$regexp = '#(src|href)="([^"]+\.(css|js|png|svg|gif|jpg|webp|woff2|mp3)(\?[^"]+)?)"#';
if (preg_match_all($regexp, $this->output, $matches, PREG_SET_ORDER)) {
	foreach ($matches as $match) {
		$file = $match[2];
		$type = $match[3];
		if ($type === 'js') {
			if ($pushJs) $headersJs[] = sprintf('<%s>; rel=preload; as=%s', $file, 'script');
		} elseif ($type === 'css') {
			if ($pushCss) $headersCss[] = sprintf('<%s>; rel=preload; as=%s', $file, 'style');
		} elseif ($type === 'woff2') {
			if ($pushFont) $headersFont[] = sprintf('<%s>; rel=preload; as=%s', $file, 'font');
		} elseif ($type === 'mp3') {
			if ($pushMp3) $headersMp3[] = sprintf('<%s>; rel=preload; as=%s', $file, 'audio');
		} else {
			if ($pushImage) $headersImage[] = sprintf('<%s>; rel=preload; as=%s', $file, 'image');
		}
	}
	$matches = null;unset($matches);
	if ($pushConsolidate) {
		$headersAll = array_merge($headersCss,$headersJs,$headersFont,$headersImage,$headersMp3);
		$headersAll = array_unique($headersAll);
		$headersAll = implode(',',$headersAll);
		if ($headersAll) header('Link: '.$headersAll, false);
	} else {
		if ($pushCss) {
			$headersCss = array_unique($headersCss);
			if ($pushIndividual) {
				foreach($headersCss as $headerCss) [b]header('Link: '.$headerCss, false);[/b]
			} else {
				$headersCss = implode(',',$headersCss);
				if ($headersCss) header('Link: '.$headersCss, false);
			}
		}
		if ($pushJs) {
			$headersJs = array_unique($headersJs);
			if ($pushIndividual) {
				foreach($headersJs as $headerJs) [b]header('Link: '.$headerJs, false);[/b]
			} else {
				$headersJs = implode(',',$headersJs);
				if ($headersJs) header('Link: '.$headersJs, false);
			}
		}
		if ($pushFont) {
			$headersFont = array_unique($headersFont);
			if ($pushIndividual) {
				foreach($headersFont as $headerFont) [b]header('Link: '.$headerFont, false;[/b]
			} else {
				$headersFont = implode(',',$headersFont);
				if ($headersFont) header('Link: '.$headersFont, false);
			}
		}
		if ($pushImage) {
			$headersImage = array_unique($headersImage);
			if ($pushIndividual) {
				foreach($headersImage as $headerImage) [b]header('Link: '.$headerImage, false);[/b]
			} else {
				$headersImage = implode(',',$headersImage);
				if ($headersImage) header('Link: '.$headersImage, false);
			}
		}
		if ($pushMp3) {
			$headersMp3 = array_unique($headersMp3);
			if ($pushIndividual) {
				foreach($headersMp3 as $headerMp3) [b]header('Link: '.$headerMp3, false);[/b]
			} else {
				$headersMp3 = implode(',',$headersMp3);
				if ($headersMp3) header('Link: '.$headersMp3, false);
			}
		}
	}
}
// HTTP2 push headers

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by letxobnav » Thu Feb 28, 2019 6:08 am

those are link headers.

Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


User avatar
Expert Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan

Post by decomuc » Mon Jun 01, 2020 2:47 am

Hi,
work this with OC2.3 also?
I want use the http/2 push function to make my shop more faster.

Newbie

Posts

Joined
Mon Jun 01, 2020 1:58 am

Post by letxobnav » Mon Jun 01, 2020 12:26 pm

yes

Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


User avatar
Expert Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan

Post by AndreyPopov » Tue Jun 02, 2020 3:53 am

letxobnav wrote:
Wed Feb 27, 2019 10:59 pm

for opencart V3 you can add this to the system/library/response.php class after

Code: Select all

if ($this->output) {

Code: Select all

 sprintf('<%s>; rel=preload; as=%s', $file, 'script')
provider use HTTP/2 and provide Server Push support.

try use ocmod file to modify system/library/response.php

but this construction :

Code: Select all

 sprintf('<%s>; rel=preload; as=%s', $file, 'script')
NOT accepted as correct code

more accuracy, this construction not accepted

Code: Select all

 '<%s>; rel=preload; as=%s'
any ideas how to solve?

New member

Posts

Joined
Sat Feb 04, 2017 2:53 am

Post by letxobnav » Tue Jun 02, 2020 10:09 am

provider use HTTP/2 and provide Server Push support.
Push headers work on any HTTP version but on versions 1.1 and below it does not help much.
NOT accepted as correct code
by whom?

Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


User avatar
Expert Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan

Post by AndreyPopov » Tue Jun 02, 2020 5:54 pm

letxobnav wrote:
Tue Jun 02, 2020 10:09 am
NOT accepted as correct code
by whom?
by ocmod system of OpenCart.

Code: Select all

'<%s>; rel=preload; as=%s'
if add to .ocmod.xml file than all after this become like comments

see highlighted syntax at the end of mod
1. no

Code: Select all

'<%s>; rel=preload; as=%s'
in mod. right syntax and correctly highlighted.
Image

2. added line with

Code: Select all

'<%s>; rel=preload; as=%s'
- all after this become as comment
Image

3. delete

Code: Select all

'<%s>; rel=preload; as=%s'
from line. syntax again become correctly highlighted
Image
Last edited by AndreyPopov on Tue Jun 02, 2020 8:34 pm, edited 1 time in total.

New member

Posts

Joined
Sat Feb 04, 2017 2:53 am

Post by letxobnav » Tue Jun 02, 2020 6:37 pm

then use:

Code: Select all

if ($pushJs) $headersJs[] = '<'.$file.'>; rel=preload; as=script';

Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


User avatar
Expert Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan

Post by AndreyPopov » Tue Jun 02, 2020 7:21 pm

letxobnav wrote:
Tue Jun 02, 2020 6:37 pm
then use:

Code: Select all

if ($pushJs) $headersJs[] = '<'.$file.'>; rel=preload; as=script';
also exist

Code: Select all

if ($pushCss) $headersCss[] = sprintf('<%s>; rel=preload; as=%s', $file, 'style');

Code: Select all

if ($pushFont) $headersFont[] = sprintf('<%s>; rel=preload; as=%s', $file, 'font');

Code: Select all

if ($pushMp3) $headersMp3[] = sprintf('<%s>; rel=preload; as=%s', $file, 'audio');

Code: Select all

if ($pushImage) $headersImage[] = sprintf('<%s>; rel=preload; as=%s', $file, 'image');
I try change by myself, but worry about syntax mistakes :(

may be, if you have time and wish, can you change whole part of code below for use in ocmod? please!

Code: Select all

foreach ($matches as $match) {
		$file = $match[2];
		$type = $match[3];
		if ($type === 'js') {
			if ($pushJs) $headersJs[] = sprintf('<%s>; rel=preload; as=%s', $file, 'script');
		} elseif ($type === 'css') {
			if ($pushCss) $headersCss[] = sprintf('<%s>; rel=preload; as=%s', $file, 'style');
		} elseif ($type === 'woff2') {
			if ($pushFont) $headersFont[] = sprintf('<%s>; rel=preload; as=%s', $file, 'font');
		} elseif ($type === 'mp3') {
			if ($pushMp3) $headersMp3[] = sprintf('<%s>; rel=preload; as=%s', $file, 'audio');
		} else {
			if ($pushImage) $headersImage[] = sprintf('<%s>; rel=preload; as=%s', $file, 'image');
		}
	}

New member

Posts

Joined
Sat Feb 04, 2017 2:53 am

Post by OSWorX » Tue Jun 02, 2020 11:49 pm

AndreyPopov wrote:
Tue Jun 02, 2020 7:21 pm
may be, if you have time and wish, can you change whole part of code below for use in ocmod? please!
Why so complicated?!
Simply create a new file, e.g. http2_header.php
save it inside the ../system/library folder

Inside the response.php
and after

Code: Select all

if ($this->output) {
add:

Code: Select all

include 'http2_header.php';
(or include_once)

All in all 1 line to add via OCMod.

Full Stack Web Developer :: Dedicated OpenCart Development & Support DACH Region
Contact for Custom Work / Fast Support.


User avatar
Guru Member

Posts

Joined
Mon Jan 11, 2010 10:52 pm
Location - Austria

Post by AndreyPopov » Wed Jun 03, 2020 12:06 am

OSWorX wrote:
Tue Jun 02, 2020 11:49 pm

Simply create a new file, e.g. http2_header.php
save it inside the ../system/library folder
add:

Code: Select all

include 'http2_header.php';
(or include_once)
good idea! thanks


one question:
what must be at the begin and at the end if file http2_header.php for correct include?

or only code with <?php at the beginning?

New member

Posts

Joined
Sat Feb 04, 2017 2:53 am

Post by OSWorX » Wed Jun 03, 2020 12:39 am

AndreyPopov wrote:
Wed Jun 03, 2020 12:06 am
one question:
what must be at the begin and at the end if file http2_header.php for correct include?

or only code with <?php at the beginning?
1. what type is this file? (pss ... php .. so which opening tag should it have?)
2. no ?> at the end - old dev code (php 3/4)

Full Stack Web Developer :: Dedicated OpenCart Development & Support DACH Region
Contact for Custom Work / Fast Support.


User avatar
Guru Member

Posts

Joined
Mon Jan 11, 2010 10:52 pm
Location - Austria

Post by AndreyPopov » Wed Jun 03, 2020 2:36 am

OSWorX wrote:
Wed Jun 03, 2020 12:39 am
[
2. no ?> at the end - old dev code (php 3/4)

on others Opencart's 3.0 files at system/library no ?> at the end.

?> needs for compatibility?

New member

Posts

Joined
Sat Feb 04, 2017 2:53 am

Post by OSWorX » Wed Jun 03, 2020 4:25 am

AndreyPopov wrote:
Wed Jun 03, 2020 2:36 am
OSWorX wrote:
Wed Jun 03, 2020 12:39 am
[
2. no ?> at the end - old dev code (php 3/4)

on others Opencart's 3.0 files at system/library no ?> at the end.

?> needs for compatibility?
As written: NO

Code: Select all

?>
Why?
Read by yourself, e.g.:
https://stackoverflow.com/questions/199 ... d-tag?lq=1
https://www.quora.com/Why-dont-we-need- ... tag-in-PHP
https://www.php.net/manual/en/language. ... hptags.php

and many more .. (use your preferred search engine by yourself ..)

Full Stack Web Developer :: Dedicated OpenCart Development & Support DACH Region
Contact for Custom Work / Fast Support.


User avatar
Guru Member

Posts

Joined
Mon Jan 11, 2010 10:52 pm
Location - Austria

Post by AndreyPopov » Wed Jun 03, 2020 6:34 am

include not work :(
hoster settings: for inclusion (include_path='.:/usr/local/pear/php72')

Code: Select all

failed to open stream: No such file or directory in /home/priazhas/priazha-shop.com/storage/modification/system/library/response.php

New member

Posts

Joined
Sat Feb 04, 2017 2:53 am

Post by sw!tch » Wed Jun 03, 2020 7:41 am

and your include looks like? Post some code.

Full Stack Web Developer :: Send a PM for Custom Work.
Backup and learn how to recover before you make any changes!


Active Member

Posts

Joined
Sat Apr 28, 2012 2:32 pm

Post by AndreyPopov » Wed Jun 03, 2020 4:00 pm

sw!tch wrote:
Wed Jun 03, 2020 7:41 am
and your include looks like? Post some code.

Code: Select all

include 'http2_header.php';
also try

Code: Select all

include 'system/library/http2_header.php';

New member

Posts

Joined
Sat Feb 04, 2017 2:53 am
Who is online

Users browsing this forum: No registered users and 245 guests