-
-
Notifications
You must be signed in to change notification settings - Fork 328
Add blocks-frontend.js for blocks frontend scripts #135
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @amjadr360
I just reviewed your pull request and a few issues need addressing.
Also, have you tested the changes in your local environment?
Cheers!
pluginDist: resolvePlugin( '.' ), // We are in ./dist folder already so the path '.' resolves to ./dist/. | ||
yarnLockFile: resolvePlugin( 'yarn.lock' ), | ||
appPath: resolvePlugin( '.' ), | ||
// These properties only exist before ejecting: | ||
ownPath: resolveOwn( '.' ), | ||
}; | ||
// @remove-on-eject-end | ||
// @remove-on-eject-end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make sure that there is a blank line at the end of the file.
@@ -120,4 +121,4 @@ module.exports = { | |||
// stats: 'errors-only', | |||
// Add externals. | |||
externals: externals, | |||
}; | |||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another blank line issue.
@@ -148,4 +149,4 @@ module.exports = { | |||
// stats: 'errors-only', | |||
// Add externals. | |||
externals: externals, | |||
}; | |||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another blank line issue.
array(), // Dependencies, defined above. | ||
// filemtime( plugin_dir_path( __DIR__ ) . 'dist/frontend.blocks.build.js' ), // Version: File modification time. | ||
true // Enqueue the script in the footer. | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The version parameter to this function is commented out, which makes the last one its version parameter which is a mistake. Please correct it.
For more information, check this link: https://developer.wordpress.org/reference/functions/wp_enqueue_script/
Pretty sure adding a frontend je file will be a bad idea. Your code should look the same for both backend and frontend. Having separate code for both means you are doing it wrong just like we did in the classic editor. I have not yet been convinced of why both frontend and backend code can't be the same. |
@ahmadawais that's true, but I wouldn't want to include the whole blocks.build.js file on the frontend just to make the block's dynamic functionality work on frontend. That's why a separate frontend file is needed to load only code required there. |
What dynamic functionality is different on the backend?
… |
Let's use the example from the original comment: I had a similar problem which is why I stumbled upon this pull request. I created a contact form block. It doesn't do anything on the backend, the edit: () => <p>Contact form will appear here.</p> And the |
Hi guys, I am agreed with @ahmadawais somehow. blocks-frontend.js will work fine if the slider block has no options like And loading blocks-frontend.js on every page even that page does not have the block for that we are using blocks-frontend.js is waste of resources. But we need a good method to fulfill our frontend needs. Like the dynamic block slider that renders in PHP. Solution: @ahmadawais what is your opinion? |
@jedlikowski Exactly! That's where you are wrong. I don't want people to develop things like that. That's the wrong way. You must be thinking — what even am I talking about. The thing is we moved away from the old editor because it was really hard to have "What You See is What You Get" in it. We built Gutenberg so that there will be no difference between the backend and the frontend. So, that in a couple of years there won't be a backend editor at all. You'd build content at the frontend. When you do something like this edit: () => <p>Contact form will appear here.</p> You are doing it wrong. Your backend should be exactly like the frontend. That's why Gutenberg was built. That's why I chose not to add a frontend file in there — to inspire people in changing their mindset and to sort of enforcing "What You See is What You Get" probability. It's why I have not merged this PR and haven't yet met a strong enough reason for separate frontend file. |
That's not what I would do. I would keep everything in JavaScript. I'll use PHP to create a new REST API endpoint and get data from that endpoint inside the JavaScript. Stop thinking in PHP and start thinking wtih JavaScript otherwise adding JavaScript to WordPress is pretty much useless. You're basically trying to write another language in another language. That works for PHP → HTML and shouldn't work for PHP → JS. Peace! ✌️ |
@ahmadawais the edit function looks like this: edit: () => <p>Contact form will appear here.</p> only for simplicity. Of course I could render the whole contact form on the backend as well. This even would force me to extract the form rendering into an external module to not repeat the code. |
@ahmadawais Is there some misunderstanding about Gutenberg's functionality here?
React doesn't load on the Wordpress frontend, so any functionality that uses React's capabilities wouldn't run by default. Gutenberg was built with the intention that your front-end functionality would follow older practices, it seems. I'm trying to get a handle on why we would load all of the backend Javascript (a great deal of which is unnecessary) rather than load in 'interaction JS' separately. Thanks for considering my questions :). For what it's worth, we've just enqueued a separate, single JS file with some jQuery for the few blocks that need it. |
@ahmadawais I agree with you here and understand what you're trying to achieve. So I don't think it is necessary to push this. But perhaps you could help us understand something that seems to boggle our minds based on what I'm thinking and reading? If we want some sort of frontend .js functionality within our plugin (examples below) - what's the best way to implement this in the plugin? Is it possible to use the compiler to our advantage to achieve this? Or is it better to just
? Examples: I know this solution has been suggested, which worked. Thoughts on this? |
Just following along but would really love to get some feed back on the last few questions posed here. I'm specifically having issues with:
Currently we have way to load js on the frontend that is required for client side interactions for blocks. The example of a form with AJAX is a great one. |
Why we need blocks-frontend.js file and what problem it will solve?
To run scripts on front-end with src/blocks.js you can only run scripts in the admin area.
For example, I created testimonial carousel block to make carousel run in the admin area and on front-end I have to call the carousel js plugin both on front-end and backend.
That's why blocks-frontend.js file is necessary to be in the great toolkit.