Skip to content

Commit

Permalink
Merge bb9d646 into 072e252
Browse files Browse the repository at this point in the history
  • Loading branch information
svetlyak40wt committed Feb 7, 2022
2 parents 072e252 + bb9d646 commit b6254ee
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 21 deletions.
64 changes: 51 additions & 13 deletions src/dependencies.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
#:render-in-ajax-response
#:with-collected-dependencies
#:push-dependency
#:push-dependencies))
#:push-dependencies
#:cache-in-memory-p))
(in-package #:reblocks/dependencies)


Expand Down Expand Up @@ -107,10 +108,20 @@ See more information at: <https://www.w3.org/TR/SRI/>")
:initarg :path
:initform nil
:reader get-path)
;; TODO: seems this slot does not used:
(binary :type (or null t)
:initarg :binary
:initform nil
:reader is-binary))
:reader is-binary)
(cache-in-memory :type (or null t)
:initarg :cache-in-memory
:initform nil
:reader cache-in-memory-p
:documentation "When true, then on creation dependency will read file's data into the memory.
This is useful for applications, deployed as a single executable file.
Create such dependencies in compile time and store in a global variable.")
(cached-data :type (or null simple-array)
:initform nil))
(:documentation "Local dependencies are served by the same webserver which renders reblocks widgets.
Each local dependency should provide a route, which will be added to the server routing
Expand Down Expand Up @@ -272,7 +283,11 @@ a browser."
(setf (slot-value dependency
'route)
(make-route url
dependency))))
dependency)))

(when (cache-in-memory-p dependency)
(setf (slot-value dependency 'cached-data)
(alexandria:read-file-into-byte-vector (get-path dependency)))))


(defmethod initialize-instance :after ((dependency remote-dependency)
Expand Down Expand Up @@ -326,7 +341,8 @@ by infering it from URL or a path"))
(defun make-dependency (path-or-url &key system
type
integrity
crossorigin)
crossorigin
cache-in-memory)
"Creates a JavaScript dependency, served from the disk.
If the system's name is given, then the path is calculated relatively
Expand All @@ -347,13 +363,17 @@ to this system's source root."
(let ((type (or type
(infer-type-from path-or-url))))
(typecase path-or-url
(string (make-instance (if *cache-remote-dependencies-in*
'cached-remote-dependency
'remote-dependency)
:type type
:remote-url path-or-url
:integrity integrity
:crossorigin crossorigin))
(string
(when cache-in-memory
(error "CACHE-IN-MEMORY argument is supported only for local dependencies."))

(make-instance (if *cache-remote-dependencies-in*
'cached-remote-dependency
'remote-dependency)
:type type
:remote-url path-or-url
:integrity integrity
:crossorigin crossorigin))
(pathname
(let ((path path-or-url))
(when system
Expand All @@ -372,12 +392,17 @@ to this system's source root."

(make-instance 'local-dependency
:type type
:path path))))))
:path path
:cache-in-memory cache-in-memory))))))


(defmethod serve ((dependency local-dependency))
"Serves local dependency from the disk."
(values (pathname (get-path dependency))
(values (cond
((cache-in-memory-p dependency)
(slot-value dependency 'cached-data))
(t
(pathname (get-path dependency))))
(get-content-type dependency)))


Expand Down Expand Up @@ -481,6 +506,19 @@ Automatically adds a prefix depending on current webapp and widget."
:dependency dependency))


;; We need to override this internal method, to merge
;; redefined routes of redefined dependencies. Otherwise
;; they will be stacked inside OR-TEMPLATE components and
;; newer version of dependency might not work (this depends on
;; sorting inside ROUTES library):
(defmethod routes::merge-uri-templates :around ((a cons) (b cons))
(if (and (typep (car a) 'dependency-route)
(typep (car b) 'dependency-route))
;; Here we will choose a new one
(list (car b))
;; otherwise
(call-next-method)))


(defmethod reblocks/routes:serve ((route dependency-route) env)
(declare (ignorable env))
Expand Down
11 changes: 11 additions & 0 deletions src/doc/changelog.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,19 @@
"POST"
"LOADED"
"HTML"
"CSS"
"HTTP")
:external-links (("Ultralisp" . "https://ultralisp.org")))
(0.43.0 2022-02-07
"""
Changed
=======
* Argument CACHE-IN-MEMORY was added to REBLOCKS/DEPENDENCIES:MAKE-DEPENDENCY function.
It allows to compile local dependency into the memory. This way you can build a standalone
executable webserver without JS and CSS dependencies!
* jQuery and some other dependencies now are cached in memory at compilation time.
""")
(0.42.0 2022-02-06
"""
Fixed
Expand Down
1 change: 1 addition & 0 deletions src/doc/dependencies.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
(reblocks/dependencies:get-route generic-function)
(reblocks/dependencies:get-type (reader dependency))
(reblocks/dependencies:get-url generic-function)
(reblocks/dependencies:cache-in-memory-p (reader local-dependency))
(reblocks/dependencies:infer-type-from generic-function)
(reblocks/dependencies:push-dependency function)
(reblocks/dependencies:push-dependencies function)
Expand Down
15 changes: 9 additions & 6 deletions src/js/jquery.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
(defvar *js-dependencies-cache* nil)


(defun make-js-dependencies ()
(defvar *jquery-dependencies*
(list (make-dependency "src/js/jquery/jquery-3.6.0.min.js"
;; "https://code.jquery.com/jquery-3.6.0.min.js"
:system :reblocks)
;; "https://code.jquery.com/jquery-3.6.0.min.js"
:system :reblocks
:cache-in-memory t)
(make-dependency "src/js/jquery/jquery.js"
:system :reblocks)
:system :reblocks
:cache-in-memory t)

;; TODO: read code in jquery.js and learn how it uses ba-bbq and seq plugins.
;; Probably we don't need them anymore. These plugins are not working
Expand All @@ -39,12 +41,13 @@
;; :system :reblocks)

(make-dependency "src/js/jquery/progress.gif"
:system :reblocks)))
:system :reblocks
:cache-in-memory t)))


(defmethod get-dependencies ((self jquery-backend))
(log:debug "Returning dependencies for jquery backend.")
(or *js-dependencies-cache*
(setf *js-dependencies-cache*
(append (make-js-dependencies)
(append *jquery-dependencies*
(call-next-method)))))
3 changes: 1 addition & 2 deletions src/routes.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@

(defun add-route (route &key (routes *routes*))
"Inserts a new route into the routing table."
(unless (routes:match routes route)
(routes:connect routes route)))
(routes:connect routes route))


(defun add-routes (app &key (routes *routes*))
Expand Down

0 comments on commit b6254ee

Please sign in to comment.