Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
indykoning committed May 23, 2024
0 parents commit 33f4784
Show file tree
Hide file tree
Showing 24 changed files with 896 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
; This file is for unifying the coding style for different editors and IDEs.
; More information at http://editorconfig.org

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
13 changes: 13 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Path-based git attributes
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html

# Ignore all test and documentation with "export-ignore".
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/phpunit.xml.dist export-ignore
/.scrutinizer.yml export-ignore
/tests export-ignore
/CONTRIBUTING.md export-ignore
/README.md export-ignore
/server-push.png export-ignore
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
build
composer.lock
docs
vendor
.phpunit.result.cache
.phpunit.cache
31 changes: 31 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Contributing

Contributions are **welcome** and will be fully **credited**.

We accept contributions via Pull Requests on [Github](https://github.com/justbetter/laravel-http3earlyhints).


## Pull Requests

- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer).

- **Add tests!** - Your patch won't be accepted if it doesn't have tests.

- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.

- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.

- **Create feature branches** - Don't ask us to pull from your master branch.

- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.

- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.


## Running Tests

``` bash
$ phpunit
```

**Happy coding**!
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# The MIT License (MIT)

Copyright (c) 2023 JustBetter

> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
> in the Software without restriction, including without limitation the rights
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> copies of the Software, and to permit persons to whom the Software is
> furnished to do so, subject to the following conditions:
>
> The above copyright notice and this permission notice shall be included in
> all copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
> THE SOFTWARE.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Early Hints Middleware for Laravel

Early Hints is a HTTP/3 concept which allows the server to send preconnect and preload headers while it's still preparing a response.
This allows the broser to start loading these resources before the server has finished building and sending a response
[See](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/103).

This package aims to provide the _easiest_ experience for adding Early Hints to your responses.
Simply route your requests through the `AddHttp3EarlyHints` middleware and it will automatically create and attach the `Link` headers necessary to implement Early Hints for your CSS, JS and Image assets.

## Installation

You can install the package via composer:
``` bash
$ composer require justbetter/laravel-http3earlyhints
```

Next you must add the `\JustBetter\Http3EarlyHints\Middleware\AddHttp3EarlyHints`-middleware to the kernel. Adding it to the web group is recommeneded as API's do not have assets to push.
```php
// app/Http/Kernel.php

...
protected $middlewareGroups = [
'web' => [
...
\JustBetter\Http3EarlyHints\Middleware\AddHttp3EarlyHints::class,
...
],
...
];
```

## Publish config

```php
php artisan vendor:publish --provider="JustBetter\Http3EarlyHints\ServiceProvider"
```


## Usage

When you route a request through the `AddHttp3EarlyHints` middleware, the response is scanned for any `link`, `script` or `img` tags that could benefit from being loaded using Early Hints.
These assets will be added to the `Link` header before sending the response to the client. Easy!

**Note:** To push an image asset, it must have one of the following extensions: `bmp`, `gif`, `jpg`, `jpeg`, `png`, `tiff` or `svg` and not have loading="lazy"

## Testing

``` bash
$ composer test
```

## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
61 changes: 61 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"name": "justbetter/laravel-http3earlyhints",
"description": "A HTTP3 Early Hints Middleware for Laravel",
"keywords": [
"laravel",
"laravel-http3earlyhints",
"serverpush",
"http/3",
"early hints"
],
"homepage": "https://github.com/justbetter/laravel-http3earlyhints",
"license": "MIT",
"authors": [
{
"name": "Indy Koning",
"email": "indy@justbetter.nl",
"role": "Developer"
}
],
"require": {
"php" : "^8.0",
"laravel/framework": "^10.0|^11.0",
"symfony/dom-crawler": "^6.0|^7.0",
"symfony/css-selector": "^6.0|^7.0"
},
"require-dev": {
"laravel/pint": "^1.7",
"larastan/larastan": "^2.5",
"phpstan/phpstan-mockery": "^1.1",
"phpunit/phpunit": "^10.1",
"orchestra/testbench": "^8.0|^9.0"
},
"autoload": {
"psr-4": {
"JustBetter\\Http3EarlyHints\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"JustBetter\\Http3EarlyHints\\Tests\\": "tests"
}
},
"scripts": {
"test": "phpunit",
"analyse": "phpstan",
"style": "pint --test",
"quality": [
"@test",
"@analyse",
"@style"
],
"fix-style": "pint"
},
"extra": {
"laravel": {
"providers": [
"JustBetter\\Http3EarlyHints\\ServiceProvider"
]
}
}
}
10 changes: 10 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
includes:
- ./vendor/larastan/larastan/extension.neon
- ./vendor/phpstan/phpstan-mockery/extension.neon

parameters:
paths:
- src
- tests
level: 4
checkMissingIterableValueType: false
21 changes: 21 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="Tests">
<directory>tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
</php>
</phpunit>
Loading

0 comments on commit 33f4784

Please sign in to comment.