Skip to content

fix(material-experimental): mdc-slider should not throw when rendering on the server #16993

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

Conversation

devversion
Copy link
Member

Follow-up to the initial mdc-slider prototype implementation. This commit adds the mdc-slider
to the universal-app test and also fixes issues that prevented the slider to be rendered on
the server.

…g on the server

Follow-up to the initial mdc-slider prototype implementation. This commit adds the mdc-slider
to the universal-app test and also fixes issues that prevented the slider to be rendered on
the server.
@devversion devversion added the target: patch This PR is targeted for the next patch release label Sep 6, 2019
@devversion devversion requested a review from jelbourn as a code owner September 6, 2019 10:56
@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Sep 6, 2019
// The MDC slider foundation accesses DOM globals, so we cannot initialize the
// foundation on the server. The foundation would be needed to move the thumb
// to the proper position and to render the ticks.
this._foundation.init();
Copy link
Member Author

@devversion devversion Sep 6, 2019

Choose a reason for hiding this comment

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

@mmalerba @jelbourn Unfortunately there is an issue with MDC slider (and I guess it can be for other MDC foundations too). The slider thumb is moved in the MDC foundation through calculated pixels. This requires the foundation to perform element measurements using getBoundingClientRect.

In the server though, getBoundingClientRect is not available and therefore the foundation cannot move the thumb to the proper location. This is an issue for server-side rendering because the pre-rendered page will have sliders that do not reflect the initial value (until the page is hydrated)

Ideally, the slider foundation would not depend on the measurements of the slider element for moving the thumb. It should only depend on the measurements for determining the value from the click position. should I create an issue on their repo?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I would create an issue on their repo. They've said that they want all of their components to be SSR compatible, so this sounds like a bug to me

Copy link
Member Author

Choose a reason for hiding this comment

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

For reference: here is the issue: material-components/material-components-web#5058

Copy link
Member

@crisbeto crisbeto left a comment

Choose a reason for hiding this comment

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

LGTM

// The foundation cannot be destroyed on the server, as the foundation
// has not be initialized on the server.
if (this._platform.isBrowser) {
this._foundation.destroy();
Copy link
Member

Choose a reason for hiding this comment

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

Maybe just check if (this._foundation)? That way if we add another condition to the initialization logic it won't break.

Copy link
Member Author

Choose a reason for hiding this comment

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

The foundation is always created, so the check would always be true. If we would only create the foundation on the browser, I guess we'd need to make it "null"-able and handle it in more places.

Copy link
Member

@jelbourn jelbourn left a comment

Choose a reason for hiding this comment

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

LGTM

@jelbourn jelbourn added pr: lgtm action: merge The PR is ready for merge by the caretaker labels Sep 6, 2019
@jelbourn jelbourn merged commit 99e66b8 into angular:master Sep 6, 2019
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Oct 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement target: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants