Skip to content

Make app() function synchronous. - #275

Merged
jorgebucaran merged 1 commit into
masterfrom
feat/sync-app
Jul 4, 2017
Merged

jorgebucaran merged 1 commit into
masterfrom
feat/sync-app

Conversation

@jorgebucaran

@jorgebucaran jorgebucaran commented Jul 3, 2017 •

Copy link
Copy Markdown
Collaborator

This will break your app if you were loading your bundle inside the <head> instead of at the bottom of <body>.

If your use case involves loading an app in the <head>, you can simply do follows:

addEventListener("DOMContentLoaded", function() {
  app({ /* state, view, actions, events */ })
})

Also, loaded was renamed to ready. Now that app is sync and DOMContentLoaded is not being used, events.ready makes more sense.

@codecov

codecov Bot commented Jul 3, 2017 •

Copy link
Copy Markdown

Codecov Report

Merging #275 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #275   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           3      3           
  Lines         146    143    -3     
  Branches       44     44           
=====================================
- Hits          146    143    -3
Impacted Files Coverage Δ
src/app.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f468dc0...feb0caf. Read the comment docs.

@FlorianWendelborn FlorianWendelborn left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM

What's the recommended event to attach to when the script is loaded in the header? load?

@andyrj

andyrj commented Jul 3, 2017

Copy link
Copy Markdown
Contributor

I like this, simplifies ssr won't need to add document.readyState.

@jorgebucaran

jorgebucaran commented Jul 3, 2017 •

Copy link
Copy Markdown
Collaborator Author

@dodekeract What's the recommended event to attach to when the script is loaded in the header? load?

I think using "DOMContentLoaded" is good enough. 🤔

addEventListener("DOMContentLoaded", () => app({ state, view, actions })

@SkaterDad Can you have a look? 🙏

Comment thread src/app.js Outdated
} else {
addEventListener("DOMContentLoaded", load)
}
return emit("loaded", emit, render(state, view))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this guaranteed to fire only when DOMContentLoaded?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

No, it won't. You need to use DOMContentLoaded if you are unsure like if you are loading your app bundle inside the <head>. You don't often do that, yes?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Haha, that is fine.. you are right. I don't do it often. Some people might, for them it will break so I guess some explicit makes sure to put the script before the closing body tag wording in the docs wouldn't go a miss.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

or put defer in your script tag if it's in the head, they become equivalent ;) I really like putting scripts in head so I can just target document.body for render

@jorgebucaran

Copy link
Copy Markdown
Collaborator Author

@andyrj Yes, very likely.

@SkaterDad SkaterDad left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good to me! As @andyrj said, this will simplify SSR, and should simplify testing also.

I did like the original behavior for ease-of-use, but the benefits seem worth the trade-off.

Will sites like Codepen require the DOMContentLoaded callback?

@jorgebucaran jorgebucaran mentioned this pull request Jul 4, 2017
@jorgebucaran

Copy link
Copy Markdown
Collaborator Author

@SkaterDad Will sites like Codepen require the DOMContentLoaded callback?

I don't know, but I don't think so. We used to have pens before this feature was originally added.

@lukejacksonn Do you approve of these changes?

@jamen jamen left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I would have thought to keep it async. But, it does knock off a few bytes, gives more user choice, and helps SSR... So I'm for it. 👍

@jorgebucaran

Copy link
Copy Markdown
Collaborator Author

Thanks @jamen.

@jorgebucaran

Copy link
Copy Markdown
Collaborator Author

@dodekeract What's the recommended event to attach to when the script is loaded in the header? load?

The recommended way is to use:

<script defer src="my-hyper-app-bundle.js"></script>

This little chart made it click for me.

script-defer-async

@lukejacksonn lukejacksonn left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should probably be discouraging scripts (without defer) in the head, the DOMContentLoaded solution is available for those who need it and ready is better than loaded so I say ship it.

This next release is going to have one hell of a change log 📝

@jorgebucaran
jorgebucaran force-pushed the feat/sync-app branch 2 times, most recently from 2004e26 to f8942b4 Compare July 4, 2017 13:04
- Now that app is sync and DOMContentLoaded is not being used,
  events.loaded was renamed to events.ready, which makes more
  sense.
@jorgebucaran
jorgebucaran merged commit 07ff87e into master Jul 4, 2017
@FlorianWendelborn

Copy link
Copy Markdown

@lukejacksonn Not true, I can't control the way the scripts I write will be embedded. Any restriction I add is one restriction too much.

I hope DOMContentLoaded will still be possible, otherwise we have to fork hyperapp.

@jorgebucaran

jorgebucaran commented Jul 4, 2017 •

Copy link
Copy Markdown
Collaborator Author

@dodekeract You can always use DOMContentLoaded.

To summarize, here are your three options:

  1. Use addEventListener("DOMContentLoaded", () => app(props)) and place your script in your preferred location.
  2. Place your script at the bottom of the body.
  3. Place your script in the head using <script defer src="..."></script>

@jorgebucaran
jorgebucaran deleted the feat/sync-app branch July 9, 2017 08:45
jorgebucaran added a commit that referenced this pull request Jan 7, 2018
….ready`. (#275)

- Since app() is now sync and DOMContentLoaded is not being used,
  events.loaded was also renamed to events.ready, which makes more
  sense.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants