This question already has an answer here:
I'm using Go to build a website. When serving in static files, css and js, no matter what I do the updates to files will not show. I've tried cache busting, deleting cache in my web browser, and deleting the disk cache on my computer, but no matter what ( even across different browsers ) an old version of the file is served. I've looked all over an found no answers.
To illustrate, I have a file main.css
html {
text-align:center;
}
However, the following css ( from an older file ) shows up in browser
html {
background-color:red;
}
Chrome's Developer Tools saying the css is loading in with a status of 200.
My Questions:
1) What is going on?
2) How do I fix this issue?
My code is as follows: test.go package main
import (
"html/template"
"log"
"net/http"
)
type PageVariables struct {
Name string
}
func main() {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.HandleFunc("/", HomePage)
log.Fatal(http.ListenAndServe(":8000", nil))
}
func HomePage(w http.ResponseWriter, r *http.Request){
HomePageVars := PageVariables{ //store the date and time in a struct
Name: "PDiddy",
}
t, err := template.ParseFiles("homepage.html") //parse the html file homepage.html
if err != nil { // if there is an error
log.Print("template parsing error: ", err) // log it
}
err = t.Execute(w, HomePageVars) //execute the template and pass it the HomePageVars struct to fill in the gaps
if err != nil { // if there is an error
log.Print("template executing error: ", err) //log it
}
}
homepage.html
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="./static/css/main.css">
</head>
<body>
<div class="container-fluid">
<div class="jumbotron">
<div class="row">
<h1>Welcome to HQ {{.Name}}</h1>
<div class="col-sm-4">
<h2>Here's Whats Happening</h2>
</div>
<div class="col-sm-8">
<h2>Select A Company</h2>
</div>
</div>
</div>
</div>
</body>
</html>
</div>
Well, I'm create package with full copy of you code. All work well (add screenshot).
May be you need check package structure? My tree
response.
.
├── homepage.html
├── static
│ └── css
│ └── main.css
└── test.go
2 directories, 3 files
Also I write unit-tests for you case. Please replace this lines to func init() in you test.go (for set handlers in http.DefaultServeMux
)
func init() {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.HandleFunc("/", HomePage)
}
test_test.go
this code compare equality static/css/main.css and http://localhost:8000/static/css/main.css.
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
func test(no int, name, addr, filepath string) error {
var (
err error
body, data []byte
resp *http.Response
fmtError = func(step string, err error) error {
return fmt.Errorf("Test #%d: error in testing <%s> in '%s' method
%s", no, step, name, err)
}
)
if resp, err = http.Get(addr); err != nil {
return fmtError(fmt.Sprintf("HTTP GET %s", addr), err)
} else if no == 0 {
return nil
}
if data, err = ioutil.ReadFile(filepath); err != nil {
return fmtError("read file", err)
}
if body, err = ioutil.ReadAll(resp.Body); err != nil {
return fmtError("read response data", err)
} else if string(body) != string(data) {
return fmtError("compare data", fmt.Errorf("File data and response body not equal"))
} else if err = resp.Body.Close(); err != nil {
return fmtError("HTTP response body close", err)
}
return nil
}
func TestServer(t *testing.T) {
var (
ts = httptest.NewServer(http.DefaultServeMux)
testData = []struct {
Name, Addr, File string
}{
{
Name: "Index",
Addr: "/",
File: "homepage.html",
},
{
Name: "main.css",
Addr: "/static/css/main.css",
File: "static/css/main.css",
},
}
)
defer ts.Close()
for i, tt := range testData {
if err := test(i, tt.Name, ts.URL+tt.Addr, tt.File); err != nil {
t.Fatal(err)
}
}
}