Create a gist now

Instantly share code, notes, and snippets.

@rakyll /main.go Secret
Last active Jun 18, 2018

Embed
What would you like to do?
package main
import (
"fmt"
"log"
"net/http"
)
const mainJS = `console.log("hello world");`
const indexHTML = `<html>
<head>
<title>Hello</title>
<script src="/main.js"></script>
</head>
<body>
</body>
</html>
`
func main() {
http.HandleFunc("/main.js", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, mainJS)
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
pusher, ok := w.(http.Pusher)
if ok { // Push is supported. Try pushing rather than waiting for the browser.
if err := pusher.Push("/main.js", nil); err != nil {
log.Printf("Failed to push: %v", err)
}
}
fmt.Fprintf(w, indexHTML)
})
// Run crypto/tls/generate_cert.go to generate cert.pem and key.pem.
// See https://golang.org/src/crypto/tls/generate_cert.go
log.Fatal(http.ListenAndServeTLS(":7072", "cert.pem", "key.pem", nil))
}
@HyeJong

This comment has been minimized.

Show comment
Hide comment
@HyeJong

HyeJong Nov 30, 2016

Thanks a lot!

HyeJong commented Nov 30, 2016

Thanks a lot!

@vsouza

This comment has been minimized.

Show comment
Hide comment

vsouza commented Dec 13, 2016

thanks!

@peeyushsrj

This comment has been minimized.

Show comment
Hide comment

Thanks!

@dreambo8563

This comment has been minimized.

Show comment
Hide comment
@dreambo8563

dreambo8563 Mar 30, 2018

I run the code locally, but I didn't see "push" in my console

I run the code locally, but I didn't see "push" in my console

@m90

This comment has been minimized.

Show comment
Hide comment
@m90

m90 Jun 18, 2018

As of today (18.06.2018), this does not seem to work in Chrome (v67) when it does work in Firefox (v60).

Strangely the .(http.Pusher) assertion succeeds and the .Push calls don't return errors, but Chrome will simply request each resource on a new request. Firefox only performs a single request.

Strangely https://http2.golang.org/serverpush still seems to work in all browsers.

m90 commented Jun 18, 2018

As of today (18.06.2018), this does not seem to work in Chrome (v67) when it does work in Firefox (v60).

Strangely the .(http.Pusher) assertion succeeds and the .Push calls don't return errors, but Chrome will simply request each resource on a new request. Firefox only performs a single request.

Strangely https://http2.golang.org/serverpush still seems to work in all browsers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment