Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for preview links #2184 #2187

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ type ServerConf struct {
ForwardedHeaders map[string]bool
KeepAppLive bool
PingInterval time.Duration
PreviewImage string
PreviewTitle string
PreviewDescription string
Comment on lines +55 to +57
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make these part of the https://github.com/h2oai/wave/blob/94bf37f538719ad279a41b0df1b452623d61aaa4/conf.go#L71C7-L71C7 as well to expose new options to the users.

Also document here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll get started on this as well as a demo :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Martin,

I started working on the demo. Using Meta's Open Graph debugger, it can read the custom title and description, but there are some issues with the image.
image
image

I did some more digging and there are some guidelines for adding images to preview links
https://developers.facebook.com/docs/sharing/best-practices/#precaching

How should I approach this?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The warning says all it needs are og:image:height and og:image:width props. I would first try it out to see if they help. If they do, the next step would be to validate passed image dimensions and:

  • If the image does not adhere to the recommended ratio or is too small, log warning and use the actual dimensions for the respective props.
  • If the image ratio is good and >= required dimensions, use the copy of the image resized to the expected dimensions.

Hope that makes sense.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that makes sense I'll get started on it!

}

type AuthConf struct {
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func Run(conf ServerConf) {
}))
}

webServer, err := newWebServer(site, broker, auth, conf.Keychain, conf.MaxRequestSize, conf.BaseURL, conf.WebDir, conf.Header)
webServer, err := newWebServer(site, broker, auth, conf.Keychain, conf.MaxRequestSize, conf.BaseURL, conf.WebDir, conf.Header, conf.PreviewImage, conf.PreviewTitle, conf.PreviewDescription)
if err != nil {
panic(err)
}
Expand Down
13 changes: 11 additions & 2 deletions web_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func newWebServer(
baseURL string,
webDir string,
header http.Header,
previewImage string,
previewTitle string,
previewDescription string,
) (*WebServer, error) {

// read default index.html page from the web root
Expand All @@ -58,15 +61,21 @@ func newWebServer(
return nil, fmt.Errorf("failed reading default index.html page: %v", err)
}

fs := handleStatic([]byte(mungeIndexPage(baseURL, string(indexPage))), http.StripPrefix(baseURL, http.FileServer(http.Dir(webDir))), header)
fs := handleStatic([]byte(mungeIndexPage(baseURL, string(indexPage), previewImage, previewTitle, previewDescription)), http.StripPrefix(baseURL, http.FileServer(http.Dir(webDir))), header)
if auth != nil {
fs = auth.wrap(fs)
}
return &WebServer{site, broker, fs, keychain, maxRequestSize, baseURL}, nil
}

func mungeIndexPage(baseURL, html string) string {
func mungeIndexPage(baseURL, html string, previewImage string, previewTitle string, previewDescription string) string {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Should be enough.

Suggested change
func mungeIndexPage(baseURL, html string, previewImage string, previewTitle string, previewDescription string) string {
func mungeIndexPage(baseURL, html, previewImage, previewTitle, previewDescription string) string {

// HACK
// add meta tags for preview image, title, description to the html string
metaTags := fmt.Sprintf(`<meta property="og:image" content="%s">
<meta property="og:title" content="%s">
<meta property="og:description" content="%s">`, previewImage, previewTitle, previewDescription)
mturoci marked this conversation as resolved.
Show resolved Hide resolved
html = strings.Replace(html, "<head>", "<head>"+metaTags, 1)

// set base URL as a body tag attribute, to be used by the front-end for deducing hash-routing and websocket addresses.
html = strings.Replace(html, "<body", `<body data-base-url="`+baseURL+`"`, 1)
// ./wave-static/a/b/c.d -> /base-url/wave-static/a/b/c.d
Expand Down