Iris
Getting Started
  • What is Iris
  • 📌Getting started
    • Installation
    • Quick start
  • 🔌Routing
    • Middleware
    • API Versioning
  • 🗜️Compression
    • Index
  • ✈️Redirect
    • Redirect from Context
    • Rewrite Middleware
    • Multi Application Instances
  • 🖼️ View/Templates
    • Documentation
    • Benchmarks
    • ➲ Examples
  • 📁File Server
    • Introduction
    • Listing
    • In-memory Cache
    • HTTP/2 Push + Embedded + Cache and Compression
    • The PrefixDir function
    • Serve files from Context
    • ➲ Examples
  • 🌎Localization
    • Documentation
    • Sitemap
    • ➲ Examples
  • 🛡️Security
    • Basic Authentication
    • CORS
    • Sessions & Cookies
    • CSRF
    • JSON Web Tokens
    • Access Control
    • Anti-bot CAPTCHA
    • ➲ Examples
  • 🚀Responses
    • Text
    • HTML
    • Markdown
    • XML
    • YAML
    • Binary
    • JSON
    • JSONP
    • Problem
    • Protocol Buffers
    • MessagePack
    • Gzip
    • Content Negotiation
    • Stream
    • Server-Sent Events
    • HTTP/2 Push
    • Recorder
    • Outroduction
    • ➲ Examples
  • 📥Requests
    • URL Query
    • Headers
    • URL Path Parameters
    • Form
    • Text
    • XML
    • YAML
    • Binary
    • JSON
    • Validation
    • Protocol Buffers
    • MessagePack
    • Gzip
    • ➲ Examples
  • 💉Dependency Injection
    • Documentation
    • Register Dependency from Context
    • Inputs
    • Outputs
    • ➲ Examples
  • 🦏MVC
    • Quick start
    • Documentation
    • Handle Errors
    • Sessions
    • Websockets
    • gRPC
    • ➲ Examples
  • 🤓Resources
    • Examples
    • Starter Kits
    • Publications
    • Benchmarks
    • Support
  • 📘Contents
    • Host
      • Automatic Public Domain with TLS
    • Configuration
    • Routing
      • Path Parameter Types
      • Reverse Lookups
      • Handle HTTP errors
      • Subdomains
      • Wrap the Router
      • Context Methods
    • HTTP Method Override
    • HTTP Referrer
    • URL Query Parameters
    • Forms
    • Model Validation
    • Cache
    • Cookies
    • Sessions
      • Database
      • Flash Messages
    • Websockets
    • Sitemap
    • Localization
    • Testing
Powered by GitBook
On this page

Was this helpful?

  1. 🚀Responses

HTTP/2 Push

Content-Type: *

Server push lets the server preemptively "push" website assets to the client without the user having explicitly asked for them. When used with care, we can send what we know the user is going to need for the page they’re requesting.

The target must either be an absolute path (like "/path") or an absolute URL that contains a valid host and the same scheme as the parent request. If the target is a path, it will inherit the scheme and host of the parent request.

Context.ResponseWriter().Push(target string, opts *http.PushOptions) error

The Push method returns iris.ErrPushNotSupported if the client has disabled push or if push is not supported on the underlying connection.

Example

The Push feature works only on HTTP/2 servers.

Create the project structure, e.g.

│   main.go
└───public
│      main.js

The main.js contains a simple alert function:

window.alert("javascript loaded");

Execute the following command to generate sample server keys:

$ openssl req -new -newkey rsa:4096 -x509 -sha256 \
-days 365 -nodes -out mycert.crt -keyout mykey.key

Create the main.go file and copy-paste the code below:

package main

import "github.com/kataras/iris/v12"

func main() {
	app := iris.New()
	app.Get("/", pushHandler)
	app.Get("/main.js", simpleAssetHandler)

	app.Run(iris.TLS("127.0.0.1:443", "mycert.crt", "mykey.key"))
}

func pushHandler(ctx iris.Context) {
	target := "/main.js"
	err := ctx.ResponseWriter().Push(target, nil)
	if err != nil {
		if err == iris.ErrPushNotSupported {
			ctx.StopWithText(iris.StatusHTTPVersionNotSupported,
				"HTTP/2 push not supported.")
		} else {
			ctx.StopWithError(iris.StatusInternalServerError, err)
		}
		return
	}

	ctx.HTML(`<html><body><script src="%s"></script></body></html>`, target)
}

func simpleAssetHandler(ctx iris.Context) {
	ctx.ServeFile("./public/main.js")
}

Run the server:

$ go run main.go

Now, open your browser's developer tools and click the Network tab. Navigate to https://127.0.0.1/, the main.js Initiator should be Push / (index) as shown below:

PreviousServer-Sent EventsNextRecorder

Last updated 2 years ago

Was this helpful?