Git Product home page Git Product logo

Comments (4)

tdewolff avatar tdewolff commented on July 16, 2024 1

I've added a method List that will return a list of resources used by the given reader:

r, _ := os.Open("www/index.html")

uris, err := p.List(r, "www.example.com", "/index.html")
if err != nil {
    panic(err)
}

cache["/index.html"] = uris

Then you could push them yourself:

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    if pusher, ok := w.(http.Pusher); ok {
        if uris, ok := cache[r.RequestURI]; ok {
            for _, uri := range uris {
                _ = pusher.Push(uri, nil)
            }
        }
    }

    // ...
}

Make sure you cache only HTML files for example, so that Push doesn't fail due to a recursive push.

from push.

tdewolff avatar tdewolff commented on July 16, 2024 1

I pushed a big refactor, which makes the distinction clearer between regular parsing and recursive.

For example, parse a file and list its URIs:

r, _ := os.Open("index.html")

parser, err := push.NewParser("example.com", "/", "/index.html")
if err != nil {
    panic(err)
}

uris, err := push.List(parser, r, "text/html")
if err != nil {
    panic(err)
}

Parse a file, read and parse its resources and list all URIs:

r, _ := os.Open("index.html")

parser, err := push.NewRecursiveParser("example.com", "/", push.FileOpenerFunc(func(uri string) (io.Reader, string, error) {
    // open file for uri
    return r, mimetype, nil
}), "/index.html")
if err != nil {
    panic(err)
}

uris, err := push.List(parser, r, "text/html")
if err != nil {
    panic(err)
}

Maybe I'll abstract away the whole recursive parsing into a type that fullfills the URIHandler interface, which simplifies the code.

from push.

mholt avatar mholt commented on July 16, 2024

That's a good start -- can we disable parsing of other types of assets so that we can cache everything that we push for (CSS, etc.)? I don't want to cause a failure with the recursive push but CSS files are also common enough that I'd like to cache them too. Or maybe if there's some better way to handle it and not take a perf hit.

from push.

mholt avatar mholt commented on July 16, 2024

Excellent; I'm going to start looking into integrating this with @wendigo's PR over at caddyserver/caddy#1215. Thank you!

from push.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.